gcc and clang implicitly instantiate template arguments during operator overload resolution

前端 未结 2 1668
梦毁少年i
梦毁少年i 2020-12-14 01:04

Consider this code:

struct A; // incomplete type

template
struct D { T d; };

template 
struct B { int * p = nullptr; };

int          


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 02:07

    Well, I think in Visual Studio 2013 code should look like(without = nullptr):

      struct A; // incomplete type
    
      template
      struct D { T d; };
    
      template 
      struct B { int * p; };
    
      int void_main() {
        B> u, v;
        u = v;          //  compiles
        u.operator=(v); // compiles
        return 0;
        }
    

    In this case it should compile well just because incomplete types can be used for specific template class specialization usage.

    As for the run-time error - the variable v used without initialization - it's correct - struct B does not have any constructor => B::p is not initialized and could contain garbage.

提交回复
热议问题