Syntax for an instance of a class template as a non-type template parameter

Violet Giraffe

I can't find the right syntax even after reading cppreference on template params. The following doesn't compile, but hopefully describes what I want to do. What's the correct syntax?

template <class DisplayType>
class DisplayAdapter : public DisplayType
{

};

template<template <typename> DisplayAdapter>
class Painter // Takes an instance of DisplayAdapter, not a type!
{
}

Here is how it's supposed to be used:

struct S{};

int main()
{
    DisplayAdapter<S> concreteAdapter;
    Painter<concreteAdapter> p;

    return 0;
}

Here's Ideone snippet for the whole thing: https://ideone.com/dvbYt8

Guillaume Racicot

What you're wanting is not a template template parameter.

A template template parameter is used to pass templates around, like this:

template<template<typename> typename Container>
struct foo {
    Container<int> container;
};

int main() {
    foo<std::vector> f;
}

As you can see, you can pass template names around with that. Remember that templates are not types, but a blueprint for type, and the language (and the standard) is not treating templates the same way as types.


I assume with your examples your trying to use non-type template parameters?

A non-type template parameter is a template parameter that is a value instead of a type. It can be of any integral types, reference type and pointer type.

For example, look at std::array:

std::array<int, 10> tenInts;

Notice the second parameter is a number. This is because std::array look something like this:

template<typename T, std::size_t N>
struct array { /* ... */ };

The second parameter is an unsigned long int.

You can also pass references and pointers as template parameter:

template<int& i> struct foo {};
template<int* i> struct bar {};

int main() {
    // Need at least internal linkage in C++14 and older
    // No linkage required since C++17, only static needed.
    static int num = 0;

    foo<num> f;
    bar<&num> b;
}

You can even pass a pointer to any type as reference template parameter:

struct stuff {};
template<stuff& s> struct foo;

int main() {
    static stuff s{};
    foo<s> f; // works!
}

This seem to be closer to what you wanted. However, you seem to have many many different type you want to send as template parameter, as the type of the instances you want to pass around are templated. For that you'll need C++17 template auto feature:

template<auto& da>
struct Painter {
    // ...
};

int main() {
    static DisplayAdapter<S> concreteAdapter;
    Painter<concreteAdapter> p;
}

And done!

If you don't have C++17 with you don't worry, and simply pass the display type along with your instance (C++14 example):

template<typename DT, DisplayAdapter<DT>& da>
struct Painter {};

// Internal linkage
DisplayAdapter<S> da;

int main() {
    Painter<S, da> painter;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Partial specialization of single type template parameter class template using non-type template parameter through a dependent type

Why is it called "non-type" template parameter?

pointer non-type template parameter

C++: Correct syntax for friending a template type member of template parameter?

Function template taking a template non-type template parameter

template class wrapping a arbitrary type/non-type template class

Non-type variadic template parameter

Specialize template template parameter with a non-type template parameter

Type of non-type parameter in template template class is non deducible in C++14 but deducible in C++17

Template template parameter with mixed type and non-type variadic parameters

Non-type template parameter dependent on a default template type parameter

Using non-type template template parameter in template specialisation

Non type template parameter

Class template overload based on presense of non-type parameter?

Class type non-type template parameter initialization does not compile

'Use of class template requires template arguments' error with default specified non-type template parameter value

template non-type template parameter

java template how to return function parameter class type instance with template type

Can't use a class reference as a non-type template parameter

Can I specialize an variadic template template parameter with non template class?

How to tell if template type is an instance of a template class?

Calling template function with non-type template parameter using class attribute

C++ - specialize function template on a templated class with a non type template parameter

Obtaining variadic template non-type parameter through explicit class

How to define a class template with reference type template parameter of template template parameter type

non type template parameter pack expansion

Class member names as non-type template parameter

How to create a Template function that creates an instance of the class specified as a template parameter

Alias Template with non-type template parameter