문제

I would like to know if the following is allowed:

template < class C >
void function(C&);

void function() {
  class {} local;
  function(local);
}

thanks

도움이 되었습니까?

해결책

It's not allowed right now. But it's supported in C++0x. The current Standard says at 14.3.1/2

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

That said, if the function is also local, there's no problem

void f() {
  class L {} local;
  struct C {
    static void function(L &l) {
      // ...
    }
  };
  C::function(local);
}

다른 팁

It's allowed if you use polymorphism instead of templates. Or if you don't need to extend the interface seen by function, simple inheritance will do.

void function( ABC & );

void function() {
  class special : public ABC {
      virtual void moof() {}
  } local;
  function(local);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top