在回答这个SO问题时,我在标准(已经是C++ 03,仍在C++ 11中)中发现,如果它们是形式的& id-expression(除了一些例外),你只能使用地址作为非类型模板参数.
但我无法回答为什么会这样.
14.3.2模板非类型参数[temp.arg.nontype]
非类型非模板模板参数的模板参数应为以下之一:
[...]
- 一个常量表达式(5.19),用于指定具有静态存储的对象的地址>持续时间和外部或内部链接或具有外部或内部链接的函数,包括函数模板和函数模板-id,但不包括非静态类成员,表示(忽略括号)as&id-expression,除非如果名称引用函数或数组,则可以省略&,如果相应的template-parameter是引用,则省略; [...]
(n3485,强调我的)
例:
using TFoobar = int (*)();
template < TFoobar tp > struct foo_struct{};
int foobar() { return 42; }
constexpr TFoobar pFoobar = &foobar;
foo_struct < &foobar > o0; // fine
foo_struct < pFoobar > o1; // ill-formed
Run Code Online (Sandbox Code Playgroud)
我想这与翻译阶段有关,即编译器对地址知之甚少.然而,为什么不允许这样做?它不应该是可能的编译器使用类似宏替换的东西来代替pFoobar用&foobar?
这曾经在几周前工作:
template <typename T, T t>
T tfunc()
{
return t + 10;
}
template <typename T>
constexpr T func(T t)
{
return tfunc<T, t>();
}
int main()
{
std::cout << func(10) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但现在g++ -std=c++0x说:
main.cpp: In function ‘constexpr T func(T) [with T = int]’:
main.cpp:29:25: instantiated from here
main.cpp:24:24: error: no matching function for call to ‘tfunc()’
main.cpp:24:24: note: candidate is:
main.cpp:16:14: note: template<class T, T t> T tfunc()
main.cpp:25:1: warning: control reaches …Run Code Online (Sandbox Code Playgroud)