2
template<class A=int, class B=float, class C=double> class SomeClass; 

Is it possible to specify only the last parameter with something like: SomeClass<C=long long int> ?

4
  • 1
    No, no named parameters so they have to be given in order. Commented Apr 3, 2018 at 12:12
  • you can apply similar tricks as with function parameters (eg write a SomeClassWithCasFirstParam wrapper that passes the desired defaults to the implementation) Commented Apr 3, 2018 at 12:19
  • Not out of the box, no. The Boost devs have a remarkable workaround to this in their Phoenix and Spirit libraries. Commented Apr 3, 2018 at 12:42
  • It is possible with a policy classes. But it requires some metaprogrammng. Commented Apr 3, 2018 at 16:07

1 Answer 1

5

In c++, is it possible to specify part of the parameters for a template class

Yes. If you don't specify all template parameters, then the unspecified ones will have the default (if a default has been specified).

Is it possible to specify only the last parameter

Not as such. (Just like non-template parameters,) Template parameters are positional. It is not possible to specify parameters after unspecified parameters.

You can work around this using a template alias:

template<class C=double, class A=int, class B=float> using PermutedSomeClass = SomeClass<A, B, C>; PermutedSomeClass<long long> // same as SomeClass<int, float, long long> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.