Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++_Templates - Fatal编程技术网

C++ 混合显式专门化和非专门化模板

C++ 混合显式专门化和非专门化模板,c++,templates,C++,Templates,我是一个模板新手。我有一个问题,是否有一种方法专门化一个类成员函数,将非专门化(或泛型)类型作为参数。也就是说,以下程序中的U1和U2可以是U1类型的boost::shared_ptr,而T1和T2是常规类型 #include <iostream> template <typename T1, typename T2> class X { public: template <typename U1, typename U2> void

我是一个模板新手。我有一个问题,是否有一种方法专门化一个类成员函数,将非专门化(或泛型)类型作为参数。也就是说,以下程序中的U1和U2可以是U1类型的boost::shared_ptr,而T1和T2是常规类型

#include <iostream>

template <typename T1, typename T2>
class X {
    public:
    template <typename U1, typename U2>
    void get_as(U1& source, U2& dest);
};

class Y {
};

template<> template<>
void
X<int, int>::get_as<double, double>(double& source, double& dest) {
    std::cout << "SOURCE IS " << source << std::endl;
    std::cout << "DESTINATION IS " << dest << std::endl;
}

template<> template<>
void 
X<int, int>::get_as<shared_ptr, shared_ptr>(shared_ptr<Y>& source, shared_ptr<Y>& dest) {
//some logic
}


int main()
{
    double d1 = 1.0;
    double d2 = 1.1;
    X<int, int> x;
    x.get_as(d1, d2);
    shared_ptr<Y> p1(new Y());
    shared_ptr<Y> p2(new Y());
    x.get_as(p1, p2); //Would this work?

    return 0;
}
#包括
模板
X类{
公众:
模板
无效获取组件(U1和源、U2和目标);
};
Y类{
};
模板
无效的
X::获取作为(双重源、双重目的){

std::cout示例中的代码将无法编译,因为模板参数中显示的共享\u ptr不是完整类型。要使其正常工作,可以如下修改函数:

template<> template<>
void
X<int, int>::get_as<shared_ptr<Y>, shared_ptr<Y> >(shared_ptr<Y>& source, shared_ptr<Y>& dest) {
//some logic
}

typename
模板参数可以是您想要的任何类型。我不知道这与当前的
boost::shared_ptr
有什么关系,但是如果您的
get_as
函数应该检索值,您可能需要输出参数,比如so
void get_as(U1&source,U2&dest)
上次编辑后,由于方法名称已更改,您将不再具有专门化功能,并将生成编译器错误。您能否澄清问题并描述所需的行为?
template<> template<typename T>
void
X<int, int>::get_as<shared_ptr<T>, shared_ptr<T> >(shared_ptr<T>& source, shared_ptr<T>& dest) {
//some logic
}
class Y {
};

class Z {
};

template <typename T1, typename T2>
class X {
    public:
    template <typename U1, typename U2>
    void get_as(U1& source, U2& dest);

    template <typename U>
    void get_as(shared_ptr<U> source, shared_ptr<U> dest);

    void get_as(shared_ptr<Y> source, shared_ptr<Y> dest);
};



template<> template<>
void
X<int, int>::get_as<double, double>(double& source, double& dest) {
    std::cout << "SOURCE IS " << source << std::endl;
    std::cout << "DESTINATION IS " << dest << std::endl;
}

template <typename T1, typename T2>
template <typename U>
void X<T1, T2>::get_as(shared_ptr<U> source, shared_ptr<U> dest)
{
  std::cout << "Overloaded member" << std::endl;
}

template <typename T1, typename T2>
void X<T1, T2>::get_as(shared_ptr<Y> source, shared_ptr<Y> dest)
{
  std::cout << "Special overloaded member" << std::endl;
}

int main()
{
    double d1 = 1.0;
    double d2 = 1.1;
    X<int, int> x;
    x.get_as(d1, d2);
    shared_ptr<Y> p1(new Y());
    shared_ptr<Y> p2(new Y());
    x.get_as(p1, p2); //Would this work?

    shared_ptr<Z> p3(new Z());
    shared_ptr<Z> p4(new Z());
    x.get_as(p3, p4); //Would this work?

    return 0;
}
SOURCE IS 1
DESTINATION IS 1.1
Special overloaded member
Overloaded member