Visual C++ Compiler allows dependent-name as a type without “typename”?

后端 未结 2 703
盖世英雄少女心
盖世英雄少女心 2021-01-18 21:28

Today one of my friends told me that the following code compiles well on his Visual Studio 2008:

#include 
struct A
{
  static int const const_         


        
相关标签:
2条回答
  • 2021-01-18 22:12

    I'm not sure "extension" is exactly how I'd describe VC++ in this respect, but yes, gcc has better conformance in this regard.

    0 讨论(0)
  • 2021-01-18 22:18

    This is not an extension at all.

    VC++ never implemented the two phases interpretation properly:

    1. At the point of definition, parse the template and determine all non-dependent names
    2. At the point of instantiation, check that the template produces valid code

    VC++ never implemented the first phase... it's inconvenient since it means not only that it accepts code that is non-compliant but also that it produces an altogether different code in some situations.

    void foo(int) { std::cout << "int" << std::endl; }
    
    template <class T> void tfoo() { foo(2.0); }
    
    void foo(double) { std::cout << "double" << std::endl; }
    
    int main(int argc, char* argv[])
    {
      tfoo<Dummy>();
    }
    

    With this code:

    • compliant compilers will print "int", because it was the only definition available at the point of definition of the template and the resolution of foo does not depend on T.
    • VC++ will print "double", because it never bothered with phase 1

    It might seem stupid as far as differences go, but if you think about the number of includes you have in a large program, there is a risk that someone will introduce an overload after your template code... and BAM :/

    0 讨论(0)
提交回复
热议问题