Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.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++;(通过非类型模板参数的值专门化模板函数)_C++_Templates - Fatal编程技术网

C++ 用C++;(通过非类型模板参数的值专门化模板函数)

C++ 用C++;(通过非类型模板参数的值专门化模板函数),c++,templates,C++,Templates,我喜欢鸭子式的打字 template<bool b> struct A{ static template<typename V> f1(V*, [other params]); static template<typename V> f2(V*, [other params]); }; template<> template<typename T> void A<false>::f

我喜欢鸭子式的打字

template<bool b>
struct A{
  static template<typename V> f1(V*, [other params]);     
  static template<typename V> f2(V*, [other params]);            
};

template<> template<typename T>
void A<false>::f1(V*, [other params]){}

template<> template<typename T>
void A<true>::f1(V*, [other params]){
   ...some code...
}  

template<int flags>
struct V{
  void f(){
     A<flags&Some compile time conditions>::f1 (this,[params]);
     A<flags&Some compile time conditions>::f2 (this,[params]); 
  } 
};
模板
结构A{
静态模板f1(V*,[其他参数]);
静态模板f2(V*,[其他参数]);
};
模板
void A::f1(V*,[其他参数]{}
模板
无效A::f1(V*,[其他参数]){
…一些代码。。。
}  
模板
结构V{
void f(){
A::f1(此[params]);
A::f2(此[params]);
} 
};
你认为有一个更优雅的解决方案吗 (我不想在函数中添加额外的参数)

我想做一些像

template<int X> struct C{
 void f(){std::cout<<"C::f"<<std::endl;};
};


template<> struct C<0>{
};


template<int X> struct D{
 C<X> c;

 template<bool b>
 void f();

 void g(){
  f<X!=0>();
 }

};

template<>
template<int X> 
void D<X>::f<true>{
c.f();
};

template<int X>  
 template<>
 void D<X>::f<false>{};


int main(){
 D<3> ch;
 ch.g();

 D<0> cn;
 cn.g();

}
template <int X, bool B>
struct DXF;

template <int X>
struct DXF<X, true>
{
  static void f() { // B is true!
  }
};

template <int X>
struct DXF<X, false>
{
  static void f() { // B is false!
  }
};
模板结构C{
void f(){std::cout
请注意,在这种情况下,
f
不是成员模板


您可以选择的另一个选项是重载。您可以将
int
包装在某个模板的参数列表中,如下所示:

template<int X> struct D{
 C<X> c;

 void f(std::true_type*) { ... true code ... }
 void f(std::false_type_*) { ... false code ... }
 void g(){
  f((std::integral_constant<bool, X!=0>*)0);
 }
模板结构D{
C C;
void f(std::true_type*){…true code…}
void f(std::false_type_*){…false code…}
void g(){
f((标准:积分常数*)0);
}

注意,true_type和false_type只是
std::integral_constant
false
s,分别是
typedef和
false

在main()中给出一个你计划如何使用D的例子可能会有帮助。对不起,大体上我打算使用D。C只是用来制造“可能存在或不可能存在”的东西亲爱的jpalecek,谢谢。当我想使用类的成员时,问题就来了。在我的解决方案中,我总是传递一个V*,我基本上使用它作为“this”。这是我想让模板void f();在D中(它将扮演V的角色)的主要原因@是的。我认为你的解决方案是可以的。我在回答中添加了另一种可能性。谢谢你,你的第二种解决方案非常接近我,因此我想这是唯一可行的方法,我会坚持你的解决方案,谢谢
template <>
void D<0>::f() {}
template<int X> struct D{
 C<X> c;

 void f(std::true_type*) { ... true code ... }
 void f(std::false_type_*) { ... false code ... }
 void g(){
  f((std::integral_constant<bool, X!=0>*)0);
 }