gpt4 book ai didi

C++语法歧义

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:44 25 4
gpt4 key购买 nike

考虑:

void f(std::pair<bool,bool> terms = std::pair<bool,bool>(1,1)) {}

gcc 4.4 正常,gcc 4.3 提示 error: expected ',' or '...' before '>' token。修复是:

void f(std::pair<bool,bool> terms = (std::pair<bool,bool>(1,1))) {}

这是什么原因?它是 4.3 中的错误吗?

最佳答案

这是一个已知问题。它认为第二个逗号分隔参数声明。这是因为在类定义中,函数默认参数首先仅被标记化,然后仅在读取完整的类主体时才被解析。因为它因此并没有真正解析默认参数,所以它没有注意到逗号实际上是模板参数列表中的逗号。

参见 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325阅读有关它的东西。被引用

The other problem is with collecting the tokens that form the default argument expression. Default arguments which contain template-ids with more than one parameter present a difficulty in determining when the default argument finishes. Consider,

template <int A, typename B> struct T { static int i;};
class C {
int Foo (int i = T<1, int>::i);
};

The default argument contains a non-parenthesized comma. Is it required that this comma is seen as part of the default argument expression and not the beginning of another of argument declaration? To accept this as part of the default argument would require name lookup of T (to determine that the '<' was part of a template argument list and not a less-than operator) before C is complete. Furthermore, the more pathological

class D {
int Foo (int i = T<1, int>::i);
template <int A, typename B> struct T {static int i;};
};

would be very hard to accept. Even though T is declared after Foo, T is in scope within Foo's default argument expression.

关于C++语法歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5573318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com