模板化类的模板化构造函数的显式模板专门化 [英] Explicit template specialization for templated constructor of templated class

查看:163
本文介绍了模板化类的模板化构造函数的显式模板专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题可能与此重叠: C +模板化类的模板化构造函数的显式模板专门化
然而,我没有在该线程中找到解决方案。



我有一个模板化的类与模板化的构造函数:

 模板< typename首先,typename ... Rest> class var {
public:
template< typename T> var(T& t){
std :: cout< 一般< std :: endl;
}
};

但是,如果这个类被实例化为一个与参数相同类的对象(即, d喜欢调用copy-(或move-)构造函数)应该做什么具体的。所以我尝试以下:

 模板< typename首先,typename ... Rest>模板<> 
var< First,Rest ...> :: var(var< First,Rest ...>& v){
std :: cout< copy<< std :: endl;
}



当尝试使用g ++ 4.6编译时得到
错误: '''token
之前无效的显式专门化
错误:封闭类模板没有明确专用
之前的错误混淆了



问题,我不得不明确说明哪个类我想专门化的构造函数...



但是,我希望它变得清楚我想做什么。任何想法如何?

解决方案

模板构造函数不是复制构造函数:

  template< typename首先,typename ... Rest> class var {
public:
var(){};
var(const var& v){
std :: cout< copy<< std :: endl;
}
template< typename T>
var(const T& t){
std :: cout< 一般< std :: endl;
}
};

int main()
{
var< int> i0;
var< int> i1(i0);
var< int> i2(Hello);
}

给予

  copy 
general

注意: / p>

您尝试将非复制构造函数专门用作复制构造函数失败。



12.8:


类X的非模板构造函数是一个拷贝构造函数,如果它的
第一个参数是X& const X& volatile X&或const volatile
X&,没有其他参数,或者所有其他
参数都有默认参数(8.3.6)。



This question might overlap with this one: C++ explicit template specialization of templated constructor of templated class . However, I didn't find a solution in that thread.

I have a templated class with templated constructor:

template<typename First, typename ... Rest> class var {
    public:
        template<typename T> var(T& t) {
            std::cout << "general" << std::endl;
        }
};

But in case this class is instantiated with an object of the same class as parameter (i.e., we'd like to call the copy- (or move-) constructor) something specific should be done. So I tried the following:

template<typename First, typename ... Rest> template<> 
var<First, Rest...>::var(var<First, Rest...>& v) {
    std::cout << "copy" << std::endl;
}

When trying to compile this with g++ 4.6 I get error: invalid explicit specialization before ‘>’ token error: enclosing class templates are not explicitly specialized confused by earlier errors, bailing out

I see the problem, I would have to say explicitly for which class I would like to specialize the constructor...

However, I hope it became clear what I want to do. Any ideas how?

解决方案

A template constructor is no copy constructor:

template<typename First, typename ... Rest> class var {
    public:
    var() {};
    var(const var& v) {
        std::cout << "copy" << std::endl;
    }
    template<typename T>
    var(const T& t) {
        std::cout << "general" << std::endl;
    }
};

int main()
{
    var<int> i0;
    var<int> i1(i0);
    var<int> i2("Hello");
}

Gives

copy
general

Note: Added some const

Your attempt to specialize a non copy constructor as copy constructor is failing.

12.8:

A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6).

这篇关于模板化类的模板化构造函数的显式模板专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆