Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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,在这种情况下,我有一个算法类: template <typename SourceType, typename DestType> class Algorithm { bool apply() { ... return true;} }; Helper功能是否正确?有更好的方法吗?这似乎是正确的。但是您的apply不会返回任何内容,这是UB 至于改进,您可以颠倒模板参数的顺序,让DataType推断: template <template <typename, ty

在这种情况下,我有一个
算法
类:

template <typename SourceType, typename DestType>
class Algorithm {
  bool apply() { ... return true;}
};

Helper
功能是否正确?有更好的方法吗?

这似乎是正确的。但是您的
apply
不会返回任何内容,这是UB

至于改进,您可以颠倒模板参数的顺序,让
DataType
推断:

template <template <typename, typename> class AlgorithmType, typename DataType>
bool Helper(const DataType* soure, DataType* dest) {
  AlgorithmType<DataType, DataType> alg;
  return alg.apply();
}
模板
布尔帮助器(常量数据类型*资源,数据类型*目的地){
算法型alg;
返回alg.apply();
}
现在你可以用

Helper<Algorithm>(const_ptr, ptr);
Helper(const_ptr,ptr);
您可以阻止对第二个参数的推断,以使其更加可靠:

template<typename T> struct identity{ using type = T; };
template<typename T>
using block_deduction = typename identity<T>::type;

template <template <typename, typename> class AlgorithmType, typename DataType>
bool Helper(const DataType* soure, block_deduction<DataType*> dest) {
  AlgorithmType<DataType, DataType> alg;
  return alg.apply();
}
模板结构标识{using type=T;};
模板
使用block_declusion=typename identity::type;
模板
bool Helper(常量数据类型*soure,block\U dest){
算法型alg;
返回alg.apply();
}
现在你可以用

Helper<Algorithm>(const_ptr, const_ptr);
Helper(const_ptr,const_ptr);

这似乎是正确的。但是您的
apply
不会返回任何内容,这是UB

至于改进,您可以颠倒模板参数的顺序,让
DataType
推断:

template <template <typename, typename> class AlgorithmType, typename DataType>
bool Helper(const DataType* soure, DataType* dest) {
  AlgorithmType<DataType, DataType> alg;
  return alg.apply();
}
模板
布尔帮助器(常量数据类型*资源,数据类型*目的地){
算法型alg;
返回alg.apply();
}
现在你可以用

Helper<Algorithm>(const_ptr, ptr);
Helper(const_ptr,ptr);
您可以阻止对第二个参数的推断,以使其更加可靠:

template<typename T> struct identity{ using type = T; };
template<typename T>
using block_deduction = typename identity<T>::type;

template <template <typename, typename> class AlgorithmType, typename DataType>
bool Helper(const DataType* soure, block_deduction<DataType*> dest) {
  AlgorithmType<DataType, DataType> alg;
  return alg.apply();
}
模板结构标识{using type=T;};
模板
使用block_declusion=typename identity::type;
模板
bool Helper(常量数据类型*soure,block\U dest){
算法型alg;
返回alg.apply();
}
现在你可以用

Helper<Algorithm>(const_ptr, const_ptr);
Helper(const_ptr,const_ptr);

帮助程序的
函数是正确的,但您可能希望将
类型名称数据类型
作为最后一个模板参数,以便从函数调用中推断:

template <template <typename, typename> class AlgorithmType, typename DataType>
bool Helper(const DataType* soure, DataType* dest) {
  AlgorithmType<DataType, DataType> alg;
  return alg.apply();
}

或者,您可以将整个算法作为模板参数传递,让用户选择
SourceType
DestType

template <typename SourceType, typename DestType>
class Algorithm {
public:
  using source_type = SourceType;
  using dest_type = DestType;
private:
  bool apply() {}
};

template <typename Algo>
bool Helper(const typename Algo::source_type* soure, typename Algo::dest_type* dest) {
  Algo alg;
  return alg.apply();
}

Helper
函数是正确的,但您可能希望将
typename数据类型
作为最后一个模板参数,以便从函数调用中推断:

template <template <typename, typename> class AlgorithmType, typename DataType>
bool Helper(const DataType* soure, DataType* dest) {
  AlgorithmType<DataType, DataType> alg;
  return alg.apply();
}

或者,您可以将整个算法作为模板参数传递,让用户选择
SourceType
DestType

template <typename SourceType, typename DestType>
class Algorithm {
public:
  using source_type = SourceType;
  using dest_type = DestType;
private:
  bool apply() {}
};

template <typename Algo>
bool Helper(const typename Algo::source_type* soure, typename Algo::dest_type* dest) {
  Algo alg;
  return alg.apply();
}

你也可以考虑使算法独立于编译时参数,并使用类似的东西:

struct Algorithm {
   template <class Source, class Dest>
   bool operator()(Source source, Dest dest) {
       //...
       return something;
   }
};
struct算法{
模板
布尔运算符()(源-源,目标-目标){
//...
归还某物;
}
};
然后:

template <class AlgType, typename DataType>
bool Helper(const DataType* soure, DataType* dest) {
  AlgType alg;
  return alg(source, dest);
}
模板
布尔帮助器(常量数据类型*资源,数据类型*目的地){
alg型alg;
返回alg(来源、目的地);
}
要运行:

Helper<Algorithm>(source_ptr, dest_ptr);
Helper(源ptr、目标ptr);

< /代码> 您也可以考虑使算法独立于编译时参数,并使用类似的方法:

struct Algorithm {
   template <class Source, class Dest>
   bool operator()(Source source, Dest dest) {
       //...
       return something;
   }
};
struct算法{
模板
布尔运算符()(源-源,目标-目标){
//...
归还某物;
}
};
然后:

template <class AlgType, typename DataType>
bool Helper(const DataType* soure, DataType* dest) {
  AlgType alg;
  return alg(source, dest);
}
模板
布尔帮助器(常量数据类型*资源,数据类型*目的地){
alg型alg;
返回alg(来源、目的地);
}
要运行:

Helper<Algorithm>(source_ptr, dest_ptr);
Helper(源ptr、目标ptr);