gpt4 book ai didi

c++ - 转换运算符的外部定义的编译器差异

转载 作者:行者123 更新时间:2023-12-01 14:07:05 24 4
gpt4 key购买 nike

关于以下代码的格式是否正确,编译器之间似乎存在一些差异。
特别是,GCC 和 Clang 接受此代码——而 MSVC 拒绝它:

template <typename T>
class Bar{};

template <typename T>
struct Foo
{
using element_type = T;

operator Bar<element_type>();
};

template <typename T>
Foo<T>::operator Bar<element_type>()
{
return {};
}

MSVC 的拒绝消息只是一个通用的:

<source>(11): error C2065: 'element_type': undeclared identifier



只有当转换类型的模板参数是依赖模板类型时才会出现该问题,因为这可以通过更改 Bar<element_type> 来解决。至 Bar<T>Bar<typename Foo<T>::element_type> .我在 compiler explorer 上做了一个小例子来证明这一点。这似乎发生在 C++11(可能更旧,未测试)到 C++2a 之间,并且与编译器的版本或标志无关。

我知道 C++ 允许在类函数/构造函数定义的主体或参数列表中删除类型的前缀类名说明符 - 但我不确定在处理转换运算符时是否允许使用相同的短格式线外定义。

这是标准中的歧义,还是任一编译器中的错误? 我很想知道这是如何在 C++ 标准中定义的(或者不是,因为它可能是)。

编辑:更令人困惑的是,MSVC 似乎接受以下代码:

template <typename T>
struct Foo
{
using element_type = T;
operator element_type();
};

template <typename T>
Foo<T>::operator element_type() // note: no 'typename' syntax
{
return {};
}

所以这似乎不仅仅是由转换为依赖于模板的类型名称引起的问题......我怀疑这可能是 MSVC 错误而不是差异。

最佳答案

该标准在该主题上非常模糊。在您显式调用转换函数( a.operator int() )的情况下, [basic.lookup.classref]/7 说

If the id-expression is a conversion-function-id, its conversion-type-id is first looked up in the class of the object expression ([class.member.lookup]) and the name, if found, is used.



不幸的是,一个转换类型 ID 可以有任意数量的 姓名 (包括0),在这种情况下无法直接查找。 [class.qual]/1.2 将相同的非规则应用于使用 :: 的函数引用,这可能适用于也可能不适用于此类函数的类外定义(因为正常查找不适用于那里)。

存在巨大的实现分歧。在以下示例中,注释表明哪些编译器(目前在 Compiler Explorer 上的最新版本)拒绝了哪些行(通常是因为它们找不到名称):
struct X {
struct A {};
operator A();

template<class> struct B {};
using C = int;
operator B<C>();

struct D {using I=int; I i;};
operator int D::*();
operator D::I();

static float E;
operator decltype(E)();

struct G {};
operator struct G();
};

X::operator A() {throw;} // OK
X::operator B<C>() {throw;} // MSVC: B and C
X::operator int D::*() {throw;} // MSVC
X::operator D::I() {throw;} // MSVC
X::operator decltype(E)() {throw;} // MSVC
X::operator struct G() {throw;} // MSVC thinks G is ::G

void f(X x) {
x.operator A(); // Clang, MSVC
x.operator B<C>(); // GCC, Clang, ICC: C; MSVC: B and C
x.operator int D::*(); // Clang
x.operator D::I(); // Clang
x.operator decltype(E)(); // Clang, ICC, MSVC; GCC segfaults
x.operator struct G(); // GCC, Clang, MSVC think G is local to f
}

Clang 接受所有定义并拒绝所有调用,因为它根本没有实现 [basic.lookup.classref]/7 ,但是当它是转换时,它有助于在 declarator-id 中误用 [basic.scope.class]/4 -功能标识。

New rules正在澄清这个烂摊子(以及许多其他烂摊子);作为审查该论文的一部分,目前正在讨论究竟应该接受多少上述内容。我的观点是 GCC 的行为最接近预期(当然,ICE 除外)。

关于c++ - 转换运算符的外部定义的编译器差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62221235/

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