template<void(*)()> struct helper { };
struct AClass
{
static void StaticFunction() { }
};
int main()
{
struct ANestedClass
{
static void StaticFunction() { }
};
helper<&AClass::StaticFunction> ok;
helper<&ANestedClass::StaticFunction> error;
}
helper<&ANestedClass::StaticFunction> error;
helper<&AClass::StaticFunction> ok;
According to the C++ standard a template-argument for a non-type, non-template template-parameter which is a constant expression must have internal or external linkage, therefore excluding such a constant expression which has no linkage. Why is this separation made for such a template-argument ?
I see nothing practically wrong with
template<void(*)()> struct helper { };
struct AClass
{
static void StaticFunction() { }
};
int main()
{
struct ANestedClass
{
static void StaticFunction() { }
};
helper<&AClass::StaticFunction> ok;
helper<&ANestedClass::StaticFunction> error;
}
the statement
helper<&ANestedClass::StaticFunction> error;
but it is illegal beccause AClass::StaticFunction has no linkage, while the statement
According to the C++ standard a template-argument for a non-type, non-template template-parameter which is a constant expression must have internal or external linkage, therefore excluding such a constant expression which has no linkage. Why is this separation made for such a template-argument ?
On Monday, 26 June 2017 12:28:59 UTC+10, Edward Diener wrote:According to the C++ standard a template-argument for a non-type, non-template template-parameter which is a constant expression must have internal or external linkage, therefore excluding such a constant expression which has no linkage. Why is this separation made for such a template-argument ?I believe this was changed by N4268 for C++17. Comparing C++14 and C++17, the relevant text ([temp.arg.nontype]/1 in C++14, now /2) has been changed from a list of allowed things, to a list of disallowed things. In effect, the address of any static member function is now valid, irrespective of linkage, as all static member functions are converted constant expressions.