gpt4 book ai didi

c++ - 这里是否需要类型名称?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:22:52 25 4
gpt4 key购买 nike

考虑代码:

#include <memory>

template <class T, class Deleter = std::default_delete<T>>
class unique_ptr_wrapper: public std::unique_ptr<T, Deleter>
{
public:
using typename std::unique_ptr<T, Deleter>::unique_ptr;
operator T* () const {return this->get();}
};

int main()
{
unique_ptr_wrapper<int> upw{new int{42}};
}

g++5.1 编译它很好,虽然 clang++ 提示

error: typename is allowed for identifiers only

我同意我们这里没有标识符,所以可能不需要 typename。但它真的被禁止了吗?是否要求编译器至少发出诊断信息?

编辑 g++ 和 clang++ 都可以在没有 typename 的情况下编译代码。


UPDATE 这似乎是一个 g++ 错误,我报告了它 here .

最佳答案

[class.inhctor]/p1,强调我的:

A using-declaration (7.3.3) that names a constructor implicitly declares a set of inheriting constructors.

构造函数不是类型。

[温度.res]/p3-4:

3 When a qualified-id is intended to refer to a type that is not a member of the current instantiation (14.6.2.1) and its nested-name-specifier refers to a dependent type, it shall be prefixed by the keyword typename, forming a typename-specifier. If the qualified-id in a typename-specifier does not denote a type, the program is ill-formed.

4 If a specialization of a template is instantiated for a set of template-arguments such that the qualified-id prefixed by typename does not denote a type, the specialization is ill-formed. The usual qualified name lookup (3.4.3) is used to find the qualified-id even in the presence of typename.

[class.qual]/p2:

In a lookup in which function names are not ignored and the nested-name-specifier nominates a class C:

  • if the name specified after the nested-name-specifier, when looked up in C, is the injected-class-name of C (Clause 9), or

  • in a using-declaration (7.3.3) that is a member-declaration, if the name specified after the nested-name-specifier is the same as the identifier or the simple-template-id’s template-name in the last component of the nested-name-specifier,

the name is instead considered to name the constructor of class C.

应用 [class.qual] 中的“通常的限定名称查找”规则,std::unique_ptr<T, Deleter>::unique_ptr命名构造函数。它不表示类型。因此,根据上述 [temp.res] 的引用,该程序格式错误(需要诊断)。

换句话说,这似乎是一个 GCC 错误(尽管 Clang 的错误消息也可以使用一些改进)。

关于c++ - 这里是否需要类型名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30554119/

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