C++分解临时方法的特殊化模板类,是可能的
C++ factoring tempate methods specialization of a template class, is that possible?
我在一个模板类中有一个模板方法
我以前读到一个方法如果不专门化类就不能专门化
但我想将其中一些方法分解,这可能吗?
示例:
class One {
public:
static const int number = 1;
};
class Two {
public:
static const int number = 2;
};
template<typename num> class A {
private:
num n;
public:
template<typename type>
void multiplyBy(); // by 1 if <int> or 1,5 if <float>
}; // A
template<> template<> void A<One>::multiplyBy<int>() {
std::cout << 1.0*n.number << std::endl;
}
template<> template<> void A<One>::multiplyBy<float>() {
std::cout << 1.5*n.number << std::endl;
}
template<> template<> void A<Two>::multiplyBy<int>() {
std::cout << 1.0*n.number << std::endl;
}
template<> template<> void A<Two>::multiplyBy<float>() {
std::cout << 1.5*n.number << std::endl;
}
int main() {
A<One> aOne;
A<Two> aTwo;
aOne.multiplyBy<int>(); // 1
aOne.multiplyBy<float>(); // 1.5
aTwo.multiplyBy<int>(); // 2
aTwo.multiplyBy<float>(); // 3
return 0;
}
一个与stackoverflow相关的问题:模板类中模板函数的C++专业化
特别是这个注释:模板类中模板函数的C++专业化
我有没有办法对multiplyBy进行因式分解,一个用于int,另一个用于float
由于英语不是我的母语,也许我错过了一些简单的东西,也许是一种部分专业化的变通方法。
编辑:把A::n放在私人里,以更好地匹配我的问题。
您可以使用标签调度:
#include <iostream>
class One {
public:
static const int number = 1;
};
class Two {
public:
static const int number = 2;
};
template<typename num>
class A {
public:
num n;
private:
template<typename> struct Tag {};
void multiplyBy(Tag<int>) {
std::cout << 1.0*n.number << std::endl;
}
void multiplyBy(Tag<float>) {
std::cout << 1.5*n.number << std::endl;
}
public:
template<typename type>
void multiplyBy() {
multiplyBy(Tag<type>());
}
};
int main() {
A<One> aOne;
A<Two> aTwo;
aOne.multiplyBy<int>(); // 1
aOne.multiplyBy<float>(); // 1.5
aTwo.multiplyBy<int>(); // 2
aTwo.multiplyBy<float>(); // 3
return 0;
}
但我想将其中一些方法分解,这可能吗?
你可能知道你不能使用:
template<> template<> void A<One>::multiplyBy<int>() {
std::cout << 1.0*n.number << std::endl;
}
而不专门化CCD_ 1。
你可以做一些类似的事情:
#include <iostream>
class One {
public:
static const int number = 1;
};
class Two {
public:
static const int number = 2;
};
template<typename num, typename type = int> struct MultiplyBy {
static void doit(num n)
{
std::cout << 1.0*n.number << std::endl;
}
};
template<typename num> struct MultiplyBy<num, float> {
static void doit(num n)
{
std::cout << 1.5*n.number << std::endl;
}
};
template<typename num> class A {
public:
num n;
template<typename type>
void multiplyBy()
{
MultiplyBy<num, type>::doit(n);
}
};
int main() {
A<One> aOne;
A<Two> aTwo;
aOne.multiplyBy<int>(); // 1
aOne.multiplyBy<float>(); // 1.5
aTwo.multiplyBy<int>(); // 2
aTwo.multiplyBy<float>(); // 3
return 0;
}
相关文章:
- 通过递归进行因子分解
- 有人能分解一下这个c++模板的语法吗
- C++ 乔列斯基因式分解
- 如何加快本征C++中的LU分解?
- 如何将整数分解为不同的变量?
- 减少本征的 QR 分解
- Xcode 中的加速框架以获得 A 的 QR 分解,但找不到 zgeqrf
- 将无向连接图分解为两个组件
- 在 C++ 中分解数字
- 分解SWIG Python接口 - 容器会产生命名空间冲突
- 对模板参数包操作进行因素分解
- 如何修复 LU 分解?
- 动态矩阵特征分解过程中的误差
- 正在从程序内存中分解x指令
- C++错误:此上下文中不允许使用分解声明
- 计算将整数n分解为4平方和的方法的数量
- 下面的代码是如何工作的?它输出分解的数字并且功能齐全,我只是不明白它是如何做到的
- 当我使用Connect信号和插槽时,会出现此分解错误
- 用C++打印指数形式的素数分解
- C++分解临时方法的特殊化模板类,是可能的