C++ Logo

std-discussion

Advanced search

Proper behavior of block-scope function overload declarations and reflection

From: Keenan Horrigan <friedkeenan_at_[hidden]>
Date: Thu, 02 Apr 2026 03:48:53 +0000
Hello,

Today I was poking around some more with reflection, and I came up with the following code which would appear to "peel off" particular overloads out of a function's overload set, getting a reflection of one overload of that function:

void function(int integer) {
    std::printf("int\n");
}

void function(char character) {
    std::printf("char\n");
}

consteval auto get_function_int() {
    void function(int);

    return ^^function;
}

static_assert(
    identifier_of(
        parameters_of(get_function_int())[0]
    )

    == "integer"
);

int main() {
    [: get_function_int() :]('h');
}

On both GCC and the experimental Clang reflection branch, this prints "int" and the static assert succeeds: https://godbolt.org/z/5caY3rh8d

On Clang you can even have

template<typename T>
consteval auto get_function() {
    void function(T);

    return ^^function;
}

And then get at the overload generically like 'get_function<int>()'. GCC currently runs into an internal error with that, though: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124756

But basically, I was wondering if this is valid behavior or not. It would be potentially useful, since I'm not aware of another way to get a reflection of a particular overload from an overload set. But I'm unsure if I could rely on this behavior.

Thanks

Received on 2026-04-02 03:49:02