基于模板参数的模板参数的类专门化

时间:2013-06-28 06:29:24

标签: c++ templates boost sfinae specialization

我有一个模板类Foo<T>,我在FooDerived<T>中派生,而模板类又是模板类Bar<T>的模板参数,即我最终得到类似{{1}的内容}}

Bar< FooDerived<T> >

我正在尝试根据template <typename T> class Foo { public: typedef T type_t; virtual double compute() { return 1.0; } }; template <typename T> class FooDerived : public Foo<T> { public: double compute() { return 42.0; } }; template <typename T> class Bar : public T { public: void f() { // This function could vary depending on U (and possibly T) std::cout << "Bar< T<U> > : " << this->compute() << std::endl; } }; 的模板参数来专门化Bar。例如,FooDerivedBar< FooDerived<int> >会有不同的行为。

Bar< FooDerived<float> >

如何在C ++ 03(或C ++ 11)中有效实现这一目标?通过有效,我的意思是我想避免无用的代码重复(真正的程序涉及比本例中更多的类型和功能)。此外,使用代码的人应该能够添加他们的专业而无需修改代码,因此任何类型的int main(void) { Bar< FooDerived<int> > a; a.f(); Bar< FooDerived<float> > b; b.f(); } - 类似的解决方案都不会被调整。

我一直在研究涉及switch的SFINAE类解决方案,例如boost::mpl boost::mpl::if_ boost::is_same检查类型,但似乎没有达到我的目标。我想这可能不适合那种模板专业化。我总是以error: redefinition of 'Bar'结束,因为编译器似乎没有将它视为专门化,例如,如果我尝试做这样的事情:

模板 class Bar:public T,private boost :: mpl :: if_,int,boost :: mpl :: false_&gt;

使用boost::mpl::if_作为私有继承或额外的模板参数似乎不能使专业化成为可能。

那么实现这样的目标的正确方法是什么?

更新1

专业化所有案例是可能的,但隐藏在这个例子背后的真正问题实际上要复杂得多。我有CachedFunction<T> : public T其中T派生自Function<U>(函数,可微函数,两次可微函数等),其中U是存储类型(密集或稀疏矩阵)。 CachedFunction包含大量函数,其实现取决于存储类型和函数类型。因此,元编程似乎是避免一些代码重复的好方法。

更新2

回应所提供的答案:我试图避免所涉及的所有案件的这些显式模板专业化。试着想象从Foo派生出3或4个类,Foo有2或3个类型,Bar包含6或7个需要不同处理的函数基于Foo的类型和考虑的派生类。基本上,对于每个ijk,我需要实现:

template<> void Bar<FooDerived_i<Type_j> >::f_k(){ ... }

因此,我试图看看是否还有其他“更清洁”的方式。

更新3

如果我使用boost::is_same,我可以做这样的事情,但是这个解决方案使得在不修改代码的情况下处理新类型变得更加困难。

这是example

#include <iostream>
#include <boost/type_traits/is_same.hpp>

typedef int type1;
typedef float type2;

template <typename T>
class Foo
{
    public:
        typedef T type_t;

        virtual double compute() { return 1.0; }
};

template <typename T>
class FooDerived
{
    public:
        typedef T type_t;

        double compute() { return 42.0; }
};

template <class T>
class Bar : public T
{
public:
    void f()
    {
        // types have to be known...
        if (boost::is_same<typename T::type_t, type1>::value)
            std::cout << "Bar< T<type1> > : " << this->compute() << std::endl;
        else if (boost::is_same<typename T::type_t, type2>::value)
            std::cout << "Bar< T<type2> > : " << this->compute() << std::endl;
    }
};

int main(void)
{
    Bar< Foo<type1> > a;
    a.f();
    Bar< FooDerived<type2> > b;
    b.f();
}

3 个答案:

答案 0 :(得分:0)

(注意,这回答了问题的第一个版本 如果您认为应将其删除,请通过评论告诉我们)

你可以这样做:

template <typename T>
class Bar : public T
{
    public:
        std::string name();
};

template<>
std::string Bar<FooDerived<int> >::name() {
  return std::string("int");
  }

template<>
std::string Bar<FooDerived<float> >::name() {
  return std::string("float");
  }

在这里测试:http://coliru.stacked-crooked.com/view?id=9054d3356f438b31b1adbb595620d838-ad7854d9cfd7979d567ca413f0830b65

感谢jogojapan的宝贵建议!

答案 1 :(得分:0)

这个怎么样?

template<>
struct Bar< FooDerived<int> >
{
   // int spezialisation here
};

template<>
struct Bar< FooDerived<float> >
{
   // float spezialisation here
};

对于只有少数具有不同行为但没有不同签名的功能,有以下几种方式:

template<>
std::string Bar<FooDerived<float> >::name() {
  return std::string("float");
}

更新

作为第二次更新的说明,我认为在这种情况下,专业化是实现不同行为的唯一干净方式。但是,如果您以更精细的方式使用模板,您当然可以重复使用大量代码:

template<class T>
std::string name_impl(T& t) {
  // generic implementationm
}

std::string name_impl(Bar<FooDerived<float>& t) {
  // implementation
}

template<class T>
std::string Bar<T>::name() {
  // something befor
  return name_impl(*this);
}

因此,您可以通过函数重载切换到编译时多态(使用inheritance,enable_if,...),这可能比模板专业更容易。在编译时这是template pattern

编辑: 如果不是您的第三次更新,我建议您阅读我的answer here。通过实施,您很可能会在方法f中遇到问题。

答案 2 :(得分:0)

正如你在一篇评论中所说,专门研究这个课程会在现实世界的例子中涉及很多代码重复,所以要避免使用。

如果你想在问题中说明,关于Foo的模板参数(或FooDerived,就此而言)的“specialize”,你应该考虑在Bar中定义模板模板参数,否则你可以例如,肯定会实例化一个Bar,因此Bar的模板参数没有模板参数。

您可以通过

执行此操作
template <template<typename> class T, typename Arg> class Bar : public T<Arg> {
public:
    std::string name()
    {
        return "Bar<T<Arg>>";
    }
};

也就是说,以下代码显示了一种方法,可用于获取专门的行为而无需专门化整个类

  1 #include <string>
  2 #include <iostream>
  3 
  4 namespace ExampleSpecializations{
  5         template <typename T>
  6         std::string name(){
  7                 return "generic";
  8         }       
  9         template <>
 10         std::string name<int>(){
 11                 return "int";
 12         }
 13 }
 14 
 15 template <typename Arg>
 16 struct Bar {
 17         std::string name()
 18         {
 19                 std::string ret = std::string("Bar<") + ExampleSpecializations::name<Arg>() + std::string(">");
 20                 return ret; 
 21         }       
 22 };      
 23 
 24 int main()
 25 {
 26         Bar<int> a;
 27         std::cout << a.name() << std::endl;
 28         Bar<char> b; 
 29         std::cout << b.name() << std::endl;
 30 }       
 31 

请注意,使用外部函数(从第4行到第13行)也提供了良好的封装,因为您没有添加不需要访问所涉及类内部任何内容的成员函数。

  1 #include <string>
  2 #include <iostream>
  3 
  4 template <typename T>
  5 struct Foo1{
  6 };              
  7                         
  8 template <typename T>   
  9 struct Foo2{
 10 };              
 11 
 12 namespace ExampleSpecializations{
 13         template <typename T>
 14                 std::string name(const T& ){
 15                         return "generic"; 
 16                 }       
 17         template <>
 18                 std::string name(const int&){
 19                         return "int";
 20                 }       
 21         template <typename T>
 22                 std::string name(const Foo1<T>& ){
 23                         std::string  ret = "Foo1<" + ExampleSpecializations::name<T>(T()) + ">";
 24                         return ret;  
 25                 }       
 26         template <typename T>
 27                 std::string name(const Foo2<T>& ){
 28                         std::string  ret = "Foo2<" + ExampleSpecializations::name<T>(T()) + ">";
 29                         return ret;  
 30                 }       
 31 }               
 32 
 33 template <typename Arg>
 34 struct Bar {
 35         std::string name()
 36         {
 37                 std::string ret = std::string("Bar<") + ExampleSpecializations::name<>(Arg()) + std::string(">");
 38                 return ret; 
 39         }       
 40 };      
 41 
 42 int main()
 43 {
 44         Bar<int> a;
 45         std::cout << a.name() << std::endl;
 46         Bar<char> b; 
 47         std::cout << b.name() << std::endl;
 48         
 49         Foo1<int> f1i;
 50         std::cout << ExampleSpecializations::name(f1i) << std::endl;
 51         
 52         Foo2<int> f2i;
 53         std::cout << ExampleSpecializations::name(f2i) << std::endl;
 54         
 55         Bar<Foo2<int> > bf2i;
 56         std::cout << bf2i.name() << std::endl;
 57 }

try the code here 不用说,我建议让Bar :: name成为非成员函数。

最后,如果您不想提供“通用”行为,则根本不提供默认实现,如:

        template <typename T>
            std::string name(const T& );

通过这种方式,前面的代码发出错误,因为在Bar上调用了“name”方法。错误解决了注释掉第47行或实现char特化。

相关问题