简体   繁体   English

类模板实例的语法作为非类型模板参数

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

I can't find the right syntax even after reading cppreference on template params. 即使在读取模板参数的cppreference之后,我也找不到正确的语法。 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 这是完整的Ideone代码段: https ://ideone.com/dvbYt8

What you're wanting is not a template template parameter. 您想要的不是模板template参数。

A template template parameter is used to pass templates around, like this: template template参数用于传递模板,如下所示:

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

std::array<int, 10> tenInts;

Notice the second parameter is a number. 注意第二个参数是一个数字。 This is because std::array look something like this: 这是因为std::array看起来像这样:

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

The second parameter is an unsigned long int. 第二个参数是一个无符号的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: 为此,您需要C ++ 17模板自动功能:

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): 如果您没有C ++ 17,请不用担心,只需将显示类型和实例一起传递(C ++ 14示例):

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

// Internal linkage
DisplayAdapter<S> da;

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

If it was some simple type like int you would just use type instead of typename keyword eg 如果它是像int这样的简单类型,则可以使用type代替typename关键字,例如

template <int x>

or 要么

template <std::vector<int>::value_type x>

But you can't put object of arbitrary type here. 但是您不能在这里放置任意类型的对象。

More about non-type template parameters can be found in another Q&A 有关非类型模板参数的更多信息,请参见其他问答。

You need to add class before DisplayAdater 您需要在DisplayAdater之前添加class

template <class DisplayType>
class DisplayAdapter : public DisplayType
{

};

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

https://godbolt.org/g/mV7YRC https://godbolt.org/g/mV7YRC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM