gpt4 book ai didi

c++ - GCC 4.4.1 是否因不接受 ctor-initializer 中的注入(inject)类名而出现错误?

转载 作者:可可西里 更新时间:2023-11-01 18:37:49 27 4
gpt4 key购买 nike

GCC 4.4.1 拒绝在 ctor-initializer 中找到我的injected-class-name:

template <typename T>
struct Base
{
Base(int x) {}
};

struct Derived : Base<int>
{
Derived() : Base(2) {}
};

int main()
{
Derived d;
}
test2.cpp: In constructor "Derived::Derived()":
test2.cpp:9: error: class "Derived" does not have any field named "Base"
test2.cpp:9: error: no matching function for call to "Base<int>::Base()"
test2.cpp:4: note: candidates are: Base<T>::Base(int) [with T = int]
test2.cpp:3: note: Base<int>::Base(const Base<int>&)

海湾合作委员会 4.8 compiles it just fine , 不过。

我确信这应该有效,而且我找不到任何不同意我的标准措辞。

这是一个 GCC 4.4.1 错误,对吧?

(我搜索了 GCC Bugzilla,但没有找到任何相关内容。)

最佳答案

是的,一个错误。

我可以在没有 ctor-initialiser 的情况下更简单地重现它:

template <typename T>
struct Base
{
};

struct Derived : Base<int>
{
Base* ptr;
};

int main()
{
Derived d;
}

/**
* in GCC 4.4.1:
*
* error: ISO C++ forbids declaration of "Base" with no type
*/

和:

[C++11: 14.6.1/4]: A lookup that finds an injected-class-name (10.2) can result in an ambiguity in certain cases (for example, if it is found in more than one base class). If all of the injected-class-names that are found refer to specializations of the same class template, and if the name is used as a template-name, the reference refers to the class template itself and not a specialization thereof, and is not ambiguous. [ Example:

template <class T> struct Base { };
template <class T> struct Derived: Base<int>, Base<char> {
typename Derived::Base b; // error: ambiguous
typename Derived::Base<double> d; // OK
};

—end example ]

请注意,我的明确用法的近等价物是“OK”。好吧,所以 Derived 在这里是一个类模板,而不是在我的例子中,所以它完全不是同一个例子。但现在我很满意 14.6.1 的全部内容使我的代码合法。

结果它被提升为 GCC bug 45515 ,但由于当时它已固定在 head 上,所以关于它的细节很少。

谢谢BoBTFish !

关于c++ - GCC 4.4.1 是否因不接受 ctor-initializer 中的注入(inject)类名而出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21118605/

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