我和一个朋友正在讨论C ++模板.他问我该怎么办:
A friend and I were discussing C++ templates. He asked me what this should do:
#include <iostream> template <bool> struct A { A(bool) { std::cout << "bool\n"; } A(void*) { std::cout << "void*\n"; } }; int main() { A<true> *d = 0; const int b = 2; const int c = 1; new A< b > (c) > (d); }main的最后一行有两个合理的解析.是'b'是模板参数还是b > (c)是模板参数?
The last line in main has two reasonable parses. Is 'b' the template argument or is b > (c) the template argument?
尽管,将其编译并查看我们得到的结果是微不足道的,但我们想知道是什么解决了歧义?
Although, it is trivial to compile this, and see what we get, we were wondering what resolves the ambiguity?
推荐答案AFAIK,它将被编译为new A<b>(c) > d.这是解析它的唯一合理的方法恕我直言.如果解析器在正常情况下不能假设>结束模板参数,那将导致更多的歧义.如果您希望以其他方式使用它,则应编写:
AFAIK it would be compiled as new A<b>(c) > d. This is the only reasonable way to parse it IMHO. If the parser can't assume under normal circumstances a > end a template argument, that would result it much more ambiguity. If you want it the other way, you should have written:
new A<(b > c)>(d);更多推荐
C ++模板歧义
发布评论