Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板参数_C++_Templates - Fatal编程技术网

C++ C++;模板参数

C++ C++;模板参数,c++,templates,C++,Templates,另外,当我在main函数中调用我的模板参数时,我得到的错误是,Bar没有默认构造函数。这是为什么?typename X只是模板参数签名的一部分,您也可以编写模板(没有T参数的名称) 因为T本身就是一个模板,所以需要先实例化它,然后才能将其用作类。签名告诉您模板需要实例化什么,在本例中,它需要一个类型名 template <template <typename X> class T> struct Bar { T data; Bar() : data() {

另外,当我在main函数中调用我的模板参数时,我得到的错误是,Bar没有默认构造函数。这是为什么?

typename X
只是模板参数签名的一部分,您也可以编写
模板
(没有T参数的名称)

因为
T
本身就是一个模板,所以需要先实例化它,然后才能将其用作类。签名告诉您模板需要实例化什么,在本例中,它需要一个类型名

template <template <typename X> class T>
struct Bar {
    T data;
    Bar() : data() { cout << "Bar" << endl; }
};
模板
结构生成
{
X数据;
};
模板
结构条
{
T sub;//用E实例化T

Bar():sub(){cout模板模板参数允许您将模板传递给其他模板。 它们不是具体的类型,需要参数化才能使其稳定

在我的Bar模板类中,它接收一个模板 参数,
表示什么

)

的模板参数名称的声明性区域 template template parameter是列表中最小的模板参数 这个名字是介绍给你的

这基本上表示该名称仅在该模板的参数列表中可用。 在许多情况下,只输入
typename
而不输入模板参数的名称就足够了,但是名称可以用来记录代码

但是,如果另一个非类型模板参数依赖于它,则给出一个名称非常有用。 对于,
模板

关于您的示例代码,第二个Bar声明有两个问题。第一个问题是Bar已经声明为接受类型,而不是模板。 您的第二个声明在声明接受模板时发生冲突

这里需要的是Bar的专门化,专门化解析为与主模板匹配的单个类型。 比如说,

template<typename X>
struct GenericThing
{
    X data;
};

template<template<typename> class T, typename E>
struct Bar
{
    T<E> sub; // instantiate T with E
    Bar() : sub() { cout << "Bar" << endl; }
};

int main()
{
    Bar<GenericThing, int> intbar;
    Bar<GenericThing, float> floatbar;
    return 0;
}

模板
无效功能模板(T&con){
用于(const auto&c:con)
标准::cout
*/
模板
无效函数模板3(V&vec){
用于(常量自动和元素:vec)
标准::cout
template<typename X>
struct GenericThing
{
    X data;
};

template<template<typename> class T, typename E>
struct Bar
{
    T<E> sub; // instantiate T with E
    Bar() : sub() { cout << "Bar" << endl; }
};

int main()
{
    Bar<GenericThing, int> intbar;
    Bar<GenericThing, float> floatbar;
    return 0;
}
template <template <typename> class T,typename Y>
struct Bar<T<Y>> {
    T<Y> data;
    Bar() : data() { cout << "Bar" << endl; }
};
#include <iostream>

using namespace std;

template <typename T = int>
struct Foo {
    T t;
    Foo() { cout << "Foo" << endl; }
};

template <typename T>
struct Baz {
    T t;
    Baz() { cout << "Baz" << endl; }
};

template <typename T>
struct Bar {
    T t;
    Bar() { cout << "Bar" << endl; }
};



template <template <typename > class T,class Y>
struct Bar<T<Y>> 
{
    T<Y> data;
    Bar() : data() { cout << "Bar Specialization" << endl; }
};

int main()
{
    Bar<Foo<>> a; //matches the specialization with T = template<typename> Foo and Y=int

    Bar<Baz<float>> b; //matches the specialization with T = template<typename> Baz and Y=float

    Bar<int> c; //matches the primary template with T=int

    return 0;
}
template <typename T>
void func_template(T& con) {
    for (const auto& c : con)
        std::cout << c << std::endl;
}

/*
        Nested Templates
        template <
            template < 
                // # of type args that will be included in the nested template
                typename,
                ...
            >
            // # of different types yo
            typename T1,
            ...
            typename TN
        >
    */

template <template<typename, typename> typename V, typename T>
void func_template3(V<T, std::allocator<T>>& vec) {
    for (const auto& elem : vec)
        std::cout << elem << std::endl;
}

template <template<typename, typename, typename, typename> typename M, typename T1, typename T2>
void func_template4(M<T1, T2, std::less<T1>, std::allocator<std::pair<const T1, T2>>> & dict) {
    for (const auto& pair : dict)
        std::cout << pair.first << " " << pair.second << std::endl;
}