简体   繁体   English

operator== 使用 msvc 编译,但不使用 gcc 和 clang

[英]operator== compiles with msvc but not with gcc and clang

I am learning C++ using the books listed here .我正在使用此处列出的书籍学习 C++。 Now, to further check that I've understood the concepts I'm also writing simple sample programs.现在,为了进一步检查我是否理解了这些概念,我还在编写简单的示例程序。 One such program that compiles with msvc but does not compile with clang and gcc is given below.下面给出了一个用 msvc 编译但不能用 clang 和 gcc 编译的程序。 Demo .演示

template<typename P> struct C{
template<typename T>
    struct E
    {
        template<typename U = P, typename V = T>
        friend bool operator==(const typename C<U>::template E<V>&, 
                               const typename C<U>::template E<V>&);
    };
};

int main()
{
    C<int>::E<double> d1, d2;
    
    std::cout<<(d1==d2);      //compiles with msvc but rejected in gcc and clang
}

So, my question is which compiler is right here(if any) according to the standard.所以,我的问题是根据标准哪个编译器在这里(如果有的话)。

MSVC is wrong in accepting the code as it is ill-formed . MSVC在接受代码时是错误的,因为它格式错误。 This can be seen from temp.param#12 which states:这可以从temp.param#12中看出:

If a friend function template declaration specifies a default template-argument, that declaration shall be a definition and shall be the only declaration of the function template in the translation unit.如果朋友 function 模板声明指定了默认模板参数,则该声明应为定义,并且应是翻译单元中 function 模板的唯一声明。

(emphasis mine) (强调我的)

And since the friend function template declaration that you've provided specifies default template argument(s) and is not a definition, the program is ill-formed.并且由于您提供的朋友 function 模板声明指定了默认模板参数并且不是定义,因此该程序格式错误。 Thus, gcc and clang and right in rejecting the program.因此,gcc 和 clang 并有权拒绝该程序。

To solve this, you can provide a definition by adding the body of the friend function template.为了解决这个问题,您可以通过添加朋友 function 模板的主体来提供定义。 Demo .演示


Here is the msvc bug report:这是 msvc 错误报告:

Invalid Friend function template operator== compiles with msvc 无效的朋友 function 模板 operator== 用 msvc 编译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MSVC:无法识别的模板声明/定义(与Clang / GCC编译) - MSVC: unrecognizable template declaration/definition (compiles with Clang/GCC) 编译类型模板谓词使用Clang编译,但不与GCC或MSVC编译 - A compile-type template predicate compiles with Clang, but not with GCC or MSVC Clang 14 和 15 显然优化了在 Clang 下按预期编译的代码 13,ICC,GCC,MSVC - Clang 14 and 15 apparently optimizing away code that compiles as expected under Clang 13, ICC, GCC, MSVC 代码用clang编译但不用gcc编译 - Code compiles with clang but not with gcc 没有GCC或MSVC的Clang - Clang without GCC or MSVC gcc和clang抛出“没有匹配的函数调用”,但msvc(cl)编译并按预期工作 - gcc and clang throw “no matching function call” but msvc (cl) compiles and works as expected gcc / clang上的模板出错,但MSVC上没有 - Error with templates on gcc/clang but not on MSVC 条件默认构造函数在 GCC 中编译,但不在 MSVC 中编译 - Conditional Default Constructor compiles in GCC but not MSVC Clang vs GCC vs MSVC模板转换运算符 - 哪个编译器是对的? - Clang vs GCC vs MSVC template conversion operator - which compiler is right? C++17 MSVC 和 Clang/GCC 之间的条件(三元)运算符不一致 - C++17 conditional (ternary) operator inconsistency between MSVC and Clang/GCC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM