Are member functions of the template class compilation when instantiating?

I found a strange problem when porting my code from Visual Studio to gcc. The following code compiles in Visual Studio, but results in an error in gcc.

namespace Baz
{
   template <class T>
   class Foo
   {
   public:
      void Bar()
      {
         Baz::Print();
      }
   }; 

   void Print() { std::cout << "Hello, world!" << std::endl; }
}

int main()
{
   Baz::Foo<int> foo;
   foo.Bar();

   return 0;
}

I understand that this should compile OK, since the class should not compile until an instance of the template is created (which, after the definition of Print ()). However, gcc reports the following:

t.cpp: In the member function 'void Baz :: Foo :: Bar ()': Line 8: error: "Print" is not a member of the "Base"

Who is right? And if gcc is right, why?

+5
source share
2 answers

gcc . , Baz , , Baz::Print Foo ( ).

, , Koenig ( ).

Baz , , ( , ), , , , . -, .

, Baz::Print Foo.

:

14.6.3

, , .

14.6.4

:

  • , .
  • , , (14.6.4.1) .

( )

Print ( ), , ( ). , ( , ), Baz int ( ), .

+4

gcc . : , , , .

, , Baz::Print , , .

T().Print() - , T, ( - . ).

, Baz:: Print, , (, T::Print), . , , , .

, Print, , - . , Print(T()) , Baz::Print(T()) . , , , , ADL, Foo<int> Print(T()) Baz::Print, ( ).

, Foo<T>::Bar Print forward-declare Print Foo.

+8

All Articles