문제

I'm trying to use an instant of a custom class as a template parameter.

class X {
public:
  X() {};
};

template <class Foo, Foo foo>
struct Bar {

};
const X x;
Bar<X, x> foo;

The compiler states that x cannot appear in a constant expression. Why that? There is everything given to construct that object at compile time.

도움이 되었습니까?

해결책

You can't do it. Standard 14.1 says:

4 A non-type template-parameter shall have one of the following (optionally cv-qualified) types:
— integral or enumeration type,
— pointer to object or pointer to function,
— reference to object or reference to function,
— pointer to member.
5 [ Note: other types are disallowed either explicitly below or implicitly by the rules governing the form of template-arguments (14.3). —end note ] The top-level cv-qualifiers on the template-parameter are ignored when determining its type.

다른 팁

As others have pointed out you can't do it. So long as you are not playing meta-programming games, the normal way to pass an actual instance of a class is in the constructor:

template <class Foo>
struct Bar {
    Bar( const Foo & f ) {
      ...
    }
};

Template parameters can be types, or integral constants. X is a type, but x is not. You also can't use constant floating point values.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top