dimanche 28 juin 2020

C++11: template function call of a template method doesn't compile? [duplicate]

The following program, if compiled as shown, outputs size: 1 align: 1.

However, trying to make the same method-template call from a templatized function doesn't work.

If I change #if 0 to #if 1 g++ 9.2.1 gives me error expected primary-expression before 'char'. clang++ gives a more helpful-sounding error: use 'template' keyword to treat 'log' as a dependent template name but I'm not sure where it wants template to occur.

So what gives?

#include <iostream>
using namespace std;



class Foo {

public:
  Foo() {};
  ~Foo() {};
  void log( int iSizeItem, int iAlignItem ) {
    cout << "size: " << iSizeItem << "  align: " << iAlignItem << endl;
  }
  
  template<class T> void log() {
    log( sizeof( T ), alignof( T ) );
  }
};


#if 0
template<class T> void Test( T& t ) {
  t.log<char>();
}
#endif


int main( int nArg, char* apszArg[] ) {
  Foo foo;
  foo.log<char>();

  //Test( foo );

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire