I have a static function defined within a struct defined within a function. I want to get a template function pointer to the inner function.
Consider the following example:
template <class Func, Func GetToken>
struct token_user {
void foo () { GetToken(); } // Do something with token.
};
struct generator_out {
constexpr static const auto get_token() {return 0;}
};
int main() {
struct generator_in {
constexpr static const auto get_token() {return 0;}
};
// Works fine
token_user<decltype(&generator_out::get_token), &generator_out::get_token>();
// Fails with GCC, works with clang and msvc
token_user<decltype(&generator_in::get_token), &generator_in::get_token>();
}
I tested this with my local MSVC 2017 Compiler and also with the clang 6.0 and gcc 8.1 compiler on wandbox.org. MSVC and clang work, gcc doesn't.
Who is correct? Are clang and msvc too forgiving and bend the c++ standard or has gcc simply not implemented this yet? (Or is it maybe even a bug?) What does the standard say about this?
EDIT: Error message from gcc:
prog.cc: In function 'int main()':
prog.cc:15:76: error: 'main()::generator_in::get_token' is not a valid template argument for type 'const int (*)()' because 'static constexpr const auto main()::generator_in::get_token()' has no linkage
token_user<decltype(&generator_in::get_token), &generator_in::get_token>(); // Fails with GCC, works with clang and msvc
error:
. Also standard defines that in-function definitions have no linkage, but i'm not sure why would it prevent gcc from using pointer to function as a template parameter. - user7860670std=c++17
- Olivier Sohn