Karsten
Karsten

Reputation: 1866

Define default implementation of template class in C++

I know it is possible to define default values for template parameters:

template<int N = 10> struct Foo {};

You can use this like Foo<> for example, but I want to be able to write just Foo.

I tried the following, but it doesn't work (throws compiler exception):

struct Foo : Foo<10> {};

Is this possible in C++?

Upvotes: 1

Views: 251

Answers (1)

JBL
JBL

Reputation: 12907

You can't directly, but can achieve something close thanks to a typedef, i.e.

template<int N = 10> struct Foo{};

typedef Foo<> DefaultFoo;//Or whatever name that fits, just not 'Foo'

int main() {
    DefaultFoo myFoo;
    return 0;
}

Upvotes: 1

Related Questions