在回答这个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
?
我想知道为什么以下代码无法编译:
struct S
{
template <typename... T>
S(T..., int);
};
S c{0, 0};
Run Code Online (Sandbox Code Playgroud)
此代码无法使用clang和GCC 4.8进行编译.这是clang的错误:
test.cpp:7:3: error: no matching constructor for initialization of 'S'
S c{0, 0};
^~~~~~~
test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
S(T..., int);
^
Run Code Online (Sandbox Code Playgroud)
在我看来,这应该工作,T应该被推断为一个长度为1的包.
如果标准禁止做这样的事情,有谁知道为什么?
遗憾的是,我无法使用 C++ 中的任何stl / std库,因为我正在为嵌入式操作系统进行编程,该操作系统仅具有可用的gcc和 4.4.4
裸 C++,因此,没有std::tuple
、std::forward
或。std::apply
std::anything_else
为了帮助理解元通用生成的代码,我提供了一个用clang编译的最小示例代码,因为它可以选择向我们显示生成的模板元编程/元编程代码。
这个问题只是出于好奇,因为我可以按照正确的顺序创建它,而不是以错误的顺序生成整数参数包。这是我用来以错误的顺序生成整数打包程序包的方法:
template<int ...>
struct MetaSequenceOfIntegers { };
template<int AccumulatedSize, typename Tn, int... GeneratedSequence>
struct GeneratorOfIntegerSequence;
template<int AccumulatedSize, typename Grouper, typename Head, typename... Tail, int... GeneratedSequence>
struct GeneratorOfIntegerSequence< AccumulatedSize, Grouper( Head, Tail... ), GeneratedSequence... >
{
typedef typename GeneratorOfIntegerSequence
< AccumulatedSize + sizeof(Head), Grouper( Tail... ), AccumulatedSize, GeneratedSequence...
>::type type;
};
template<int AccumulatedSize, typename Grouper, int... GeneratedSequence> …
Run Code Online (Sandbox Code Playgroud) c++ metaprogramming template-meta-programming variadic-templates c++11