Confusing Template error

后端 未结 4 1193
萌比男神i
萌比男神i 2020-12-04 11:44

I\'ve been playing with clang a while, and I stumbled upon \"test/SemaTemplate/dependent-template-recover.cpp\" (in the clang distribution) which is supposed to provide hint

4条回答
  •  无人及你
    2020-12-04 12:29

    In addition to the points others made, notice that sometimes the compiler couldn't make up his mind and both interpretations can yield alternative valid programs when instantiating

    #include 
    
    template
    struct A {
      typedef int R();
    
      template
      static U *f(int) { 
        return 0; 
      }
    
      static int f() { 
        return 0;
      }
    };
    
    template
    bool g() {
      A a;
      return !(typename A::R*)a.f(0);
    }
    
    
    int main() {
      std::cout << g() << std::endl;
    }
    

    This prints 0 when omitting template before f but 1 when inserting it. I leave it as an exercise to figure out what the code does.

提交回复
热议问题