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

C++ 模板简化

C++ 模板简化,c++,templates,simplification,C++,Templates,Simplification,在我的一个项目中,我有以下类模板层次结构: template <typename FruitType, typename ParentFilterType = void> class filter; template <typename FruitType> // Specialization when no parent filter is needed class filter<FruitType, void>; 显然,这有点冗长。我想知道是否有可能得

在我的一个项目中,我有以下类模板层次结构:

template <typename FruitType, typename ParentFilterType = void>
class filter;

template <typename FruitType> // Specialization when no parent filter is needed
class filter<FruitType, void>;
显然,这有点冗长。我想知道是否有可能得到更具可读性的东西。比如:

complex_filter<apple, banana, orange>::type my_apple_filter;
其中complex_filter::type将解析为filter

我尝试将complex_filter作为内部带有typedef的结构模板,但到目前为止没有成功。模板参数的数量应该在1到5之间变化

你曾经需要过类似的东西吗?我怎么能这么做

不幸的是,我不能使用C++0x,但如果有更好的解决方案,请随意发布,因为知道它总是很好的


谢谢。

您尝试的方法应该会奏效,有点:

template< class A, class B >
struct complex_filter2
{
  typedef typename filter< A, filter< B > > type;
};

template< class A, class B, class C >
struct complex_filter3
{
  typedef typename filter< A, filter< B, filter< C > > > type;
};

//etc

你所尝试的应该是有效的:

template< class A, class B >
struct complex_filter2
{
  typedef typename filter< A, filter< B > > type;
};

template< class A, class B, class C >
struct complex_filter3
{
  typedef typename filter< A, filter< B, filter< C > > > type;
};

//etc

在C++0x中,它将是可变模板

如果没有C++0x,您可以简单地使用大量参数,并提供默认值

template <typename F0, typename F1 = void, typename F2 = void, typename F3 = void>
struct complex_filter
{
  typedef filter<F0, typename complex_filter<F1, F2, F3>::type> type;
};

template <>
struct complex_filter<void,void,void,void>
{
  typedef void type;
};

然后可以根据需要使用它,如果需要更多参数,则必须手动扩展它。

在C++0x中,它将是可变模板

如果没有C++0x,您可以简单地使用大量参数,并提供默认值

template <typename F0, typename F1 = void, typename F2 = void, typename F3 = void>
struct complex_filter
{
  typedef filter<F0, typename complex_filter<F1, F2, F3>::type> type;
};

template <>
struct complex_filter<void,void,void,void>
{
  typedef void type;
};


然后可以根据需要使用它,如果需要更多参数,则必须手动扩展它。

可以使用boost吗?mpl有类型向量和元函数,可以让你做这样的事情SC++03或C++0x?在C++0x中,您可能可以使用可变模板来简化syntax@DavidRodríguez-dribeas:我在问题中说我必须使用C++03,但出于好奇,我对C++0x的解决方案持开放态度。我一定会看看可变模板。感谢您的建议。使用可变模板,很容易从某些模板xxx继承:xxx,T{};或者类似的。你能用boost吗?mpl有类型向量和元函数,可以让你做这样的事情SC++03或C++0x?在C++0x中,您可能可以使用可变模板来简化syntax@DavidRodríguez-dribeas:我在问题中说我必须使用C++03,但出于好奇,我对C++0x的解决方案持开放态度。我一定会看看可变模板。感谢您的建议。使用可变模板,很容易从某些模板xxx继承:xxx,T{};或者类似的。而且,是的,你必须专门研究每个维度。。。至少在C++0x之前!谢谢你的回答。是否有任何解决方案不需要编写N个不同的复杂过滤器结构模板@Tomalak Geret'kal:仅供学习之用:C++0x在这方面提供了什么帮助?@ereOn:C++0x提供可变模板,即处理先验未知数量的参数的能力。@ereOn:Boost。预处理器非常方便生成类似这样的重复代码。@ereOn:C++0x可以让您编写一个varadic模板,具有可变数量的参数。所以你最终会递归地写它,但你会有一个模板。而且,是的,你必须专门针对每个维度。。。至少在C++0x之前!谢谢你的回答。是否有任何解决方案不需要编写N个不同的复杂过滤器结构模板@Tomalak Geret'kal:仅供学习之用:C++0x在这方面提供了什么帮助?@ereOn:C++0x提供可变模板,即处理先验未知数量的参数的能力。@ereOn:Boost。预处理器非常方便生成类似这样的重复代码。@ereOn:C++0x可以让您编写一个varadic模板,具有可变数量的参数。因此,您将以递归方式编写它,但您将拥有一个模板。非常感谢!正是我想要的。我只是不得不用struct替换类,但我想这是一个输入错误,因为其他类型无法访问。非常感谢!正是我想要的。我只是不得不用struct替换类,但我想这是一个输入错误,因为否则类型就无法访问