简体   繁体   English

使用struct和bool进行模板专业化

[英]Template specialization with struct and bool

I have a template class in which I am specializing a couple of methods. 我有一个模板类,其中我专门介绍了几种方法。 For some reason, when I added a specialization for a struct, it seems to be conflicting with the specialization for bool. 出于某种原因,当我为结构添加特化时,它似乎与bool的特化相矛盾。 I am getting a type conversion error because it is trying to set the struct = bool (resolving to the wrong specialization). 我收到类型转换错误,因为它试图设置struct = bool(解析为错误的特化)。 Here is some code 这是一些代码

.h: 。H:

typedef struct foo {
  ...
}

template <class T> class bar {
   template <class T> void method1() {...}
   template <> void method1<bool>() {...}
   template <> void method1<foo>() {...}
}

.cpp 的.cpp

 template class bar<bool>;
 template class bar<foo>;

I am getting the error inside method1<bool> because it is setting T=foo instead of resolving it to method1<foo> . 我在method1<bool>得到错误,因为它设置T = foo而不是将其解析为method1<foo>

Any ideas? 有任何想法吗?

The first part of your code is already incorrect. 代码的第一部分已经不正确了。 C++ does not support explicit specialization of "nested" (member) templates without explicit specialization of the enclosing template. C ++不支持“嵌套”(成员)模板的显式特化,而不包含封闭模板的显式特化。

In the context of your code, it is illegal to explicitly specialize template method method1 without explicitly specializing the entire class template bar . 在代码的上下文中,明确地专门化模板方法method1而不明确专门化整个类模板bar是非法的。

If your member template function member1 depended on some parameters, you could use overloading instead of template specialization as a workaround. 如果您的成员模板函数member1依赖于某些参数,则可以使用重载而不是模板特化作为变通方法。 But since it doesn't, you have to redesign you templates somehow. 但由于它没有,你必须以某种方式重新设计模板。 What you do above is, once again, illegal in C++. 你在C ++上再次做的是非法的。

The errors you get further down can easily be (and most probably are) induced by that original problem. 您可以很容易地(并且很可能是)由原始问题引起的错误。

PS The description of the problem you posted implies that your code compiles. PS您发布的问题的描述暗示您的代码编译。 What you posted should not compile for the reasons described above. 由于上述原因,您发布的内容不应编译。 This suggests that you are posting fake code. 这表明您发布了假代码。 Post real code. 发布实际代码。

(EDITED) (编者)

You may try the following, which delegates the method implementation to a templated helper class. 您可以尝试以下方法,将方法实现委托给模板化助手类。

.h: 。H:

typedef struct Foo {
  ...
}

template<class T_Bar, class T2> struct BarMethod1;
template <class T> class Bar 
{
   template<class T2> void method1(...)
   {
      BarMethod1<Bar, T2>(...)(...);
   }
}
template <class T_Bar, class T2> class BarMethod1 
   {void operator()(...){...}};
template <class T_Bar> class BarMethod1<T_Bar, bool>
   {void operator()(...){...}};

template <class T_Bar> BarMethod1<T_Bar, Foo>
   {void operator()(...){...}};

.cpp 的.cpp

 template class Bar<bool>;
 template class BarMethod1<Bar<bool>, bool>;
 template class BarMethod1<Bar<bool>, Foo>;
 template class Bar<Foo>;
 template class BarMethod1<Bar<Foo>, bool>;
 template class BarMethod1<Bar<Foo>, Foo>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM