C++ 当模板不够灵活,并且重载会导致代码重复时,我应该怎么做

C++ 当模板不够灵活,并且重载会导致代码重复时,我应该怎么做,c++,C++,就像标题一样。我发现自己的情况是模板不够灵活,函数重载导致代码重复 让我展示一些代码: // header.h: inline void some_func(const unsigned int size, int * arr_1, int * arr_2) { // block of code #1. // -- This block is identical in every overloaded instance of this function // S


就像标题一样。我发现自己的情况是模板不够灵活,函数重载导致代码重复


让我展示一些代码:

// header.h:

inline void some_func(const unsigned int size, int * arr_1, int * arr_2) {

    // block of code #1. 
    // -- This block is identical in every overloaded instance of this function

    // Single line of code:
    // -- This line is DIFFERENT in every overloaded instance of this function
    int4 val = reinterpret_cast<int4*>(arr_1)[i];

    // block of code #2. 
    // -- This block is identical in every overloaded instance of this function
}

inline void some_func(const unsigned int size, float * arr_1, float * arr_2) {

    // block of code #1. 
    // -- This block is identical in every overloaded instance of this function

    // Single line of code:
    // -- This line is DIFFERENT in every overloaded instance of this function
    float4 val = reinterpret_cast<float4*>(arr_1)[i];

    // block of code #2. 
    // -- This block is identical in every overloaded instance of this function
}

template <typename T> inline
void some_func_warper(T * arr_1, T * arr_2) {

    // Calculate few parameters...

    // Call some_func(***)

}
//header.h:
内联void some_func(常量无符号整数大小,int*arr_1,int*arr_2){
//代码块#1。
//--此块在该函数的每个重载实例中都是相同的
//单行代码:
//--此行在该函数的每个重载实例中都是不同的
int4 val=重新解释铸造(arr_1)[i];
//代码块#2。
//--此块在该函数的每个重载实例中都是相同的
}
内联void some_func(常量无符号整数大小,浮点*arr_1,浮点*arr_2){
//代码块#1。
//--此块在该函数的每个重载实例中都是相同的
//单行代码:
//--此行在该函数的每个重载实例中都是不同的
float4 val=重新解释铸造(arr_1)[i];
//代码块#2。
//--此块在该函数的每个重载实例中都是相同的
}
模板内联
取消某些功能整经机(T*arr\u 1,T*arr\u 2){
//计算几个参数。。。
//调用一些函数(***)
}

如您所见,由于我正在对输入数组
arr\u 1
arr\u 2
进行矢量化,因此无法为
某些函数(***)
编写模板。我只能使它过载,这迫使我复制两大块代码
的情况下,某些函数整经器(***)
使用模板显然会产生误导。因此,我想避免使用这种整经机太多

问题#1:
有没有办法做到这一点:
“编写函数模板并从该模板中只指定一行”

问题2:
我很肯定问题1的答案是否定的

因此,对于这种情况是否还有其他解决办法

使用两个模板。一个用来保存泛型some_func代码,另一个专门用于该行中使用的类型。如果您在多个位置执行此矢量化,您可能会发现可以使用一个trait类来定义类型,然后在多个模板函数中使用它

template<class T>
class some_func_val_type;

template<>
class some_func_val_type<int> {
    using type = int4;
};

template<>
class some_func_val_type<float> {
    using type = float4;
};

template<class T>
inline void some_func(const unsigned int size, T * arr_1, T * arr_2) {

    // block of code #1. 
    // -- This block is identical in every overloaded instance of this function

    // Single line of code:
    typename some_func_val_type<T>::type val =
        reinterpret_cast<typename some_func_val_type<T>::type*>(arr_1)[i];

    // block of code #2. 
    // -- This block is identical in every overloaded instance of this function
}
模板
为某些函数类型初始化;
模板
为某些函数类型初始化{
使用类型=int4;
};
模板
为某些函数类型初始化{
使用类型=4;
};
模板
内联void some_func(常量无符号整数大小,T*arr_1,T*arr_2){
//代码块#1。
//--此块在该函数的每个重载实例中都是相同的
//单行代码:
typename一些函数值类型::类型值=
重新解释铸造(arr_1)[i];
//代码块#2。
//--此块在该函数的每个重载实例中都是相同的
}

使用两个模板。一个用来保存泛型some_func代码,另一个专门用于该行中使用的类型。如果您在多个位置执行此矢量化,您可能会发现可以使用一个trait类来定义类型,然后在多个模板函数中使用它

template<class T>
class some_func_val_type;

template<>
class some_func_val_type<int> {
    using type = int4;
};

template<>
class some_func_val_type<float> {
    using type = float4;
};

template<class T>
inline void some_func(const unsigned int size, T * arr_1, T * arr_2) {

    // block of code #1. 
    // -- This block is identical in every overloaded instance of this function

    // Single line of code:
    typename some_func_val_type<T>::type val =
        reinterpret_cast<typename some_func_val_type<T>::type*>(arr_1)[i];

    // block of code #2. 
    // -- This block is identical in every overloaded instance of this function
}
模板
为某些函数类型初始化;
模板
为某些函数类型初始化{
使用类型=int4;
};
模板
为某些函数类型初始化{
使用类型=4;
};
模板
内联void some_func(常量无符号整数大小,T*arr_1,T*arr_2){
//代码块#1。
//--此块在该函数的每个重载实例中都是相同的
//单行代码:
typename一些函数值类型::类型值=
重新解释铸造(arr_1)[i];
//代码块#2。
//--此块在该函数的每个重载实例中都是相同的
}

对不起,我一直收到一个错误:
错误:预期为a”;“
错误:标识符“val”未定义
也许你知道为什么吗?所有内容都与
单行代码相关
由于依赖类型,您需要在
sum\u func\u val\u type::type
之前使用
typename
。以前使用typedef可以更容易阅读。抱歉,我一直收到一个错误:
错误:预期为a”;“
错误:标识符“val”未定义
也许你知道为什么吗?所有内容都与
单行代码相关
由于依赖类型,您需要在
sum\u func\u val\u type::type
之前使用
typename
。您可以在之前使用typedef使其更易于阅读。