C ++模板歧义

编程入门 行业动态 更新时间:2024-10-26 04:22:54
本文介绍了C ++模板歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我和一个朋友正在讨论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 ++模板歧义

本文发布于:2023-11-07 06:47:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:歧义   模板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!