C++模板专用获取器
C++ template specialized getters
我目前正在编写一个使用NURBS surfaces
的程序,您可以在其中执行两个方向(U
和V
)的算法。为了避免代码重复,我尝试使用模板,但我没有使用它们的经验。这里有一个小例子来说明我想做的事情:
#include <iostream>
enum class Dir {
U, V
};
struct Foo {
unsigned cu, cv;
Foo(unsigned cu, unsigned cv) : cu(cu), cv(cv) {};
template<Dir>
const Dir otherDir();
template<>
const Dir otherDir<Dir::V>() {
return Dir::U;
}
template<>
const Dir otherDir<Dir::U>() {
return Dir::V;
}
template<Dir>
unsigned count();
template<>
unsigned count<Dir::U>() {
return cu;
}
template<>
unsigned count<Dir::V>() {
return cv;
}
template<Dir d>
unsigned totalCount() {
auto c = count<d>();
auto cOther = count<otherDir<d>()>();
return c * cOther;
}
};
int main() {
Foo f(3,2);
std::cout << (f.count<Dir::U>() == 3) << std::endl;
std::cout << (f.otherDir<Dir::U>() == Dir::V) << std::endl;
std::cout << f.totalCount<Dir::U>() << std::endl;
}
但由于 main 的最后一行(VS2015,但我认为这不是编译器的错),这无法编译:
1>...main.cpp(42): error C2672: 'Foo::count': no matching overloaded function found
1>...main.cpp(52): note: see reference to function template instantiation 'unsigned int Foo::totalCount<Dir::U>(void)' being compiled
1>...main.cpp(42): error C2975: 'unnamed-parameter': invalid template argument for 'Foo::count', expected compile-time constant expression
1>...main.cpp(27): note: see declaration of 'unnamed-parameter'
1>...main.cpp(43): error C3536: 'cOther': cannot be used before it is initialized
我接近上述功能的唯一方法是将主要方向和其他方向指定为模板参数,如下所示:
struct Foo {
...
template<Dir d, Dir otherD>
unsigned totalCount() {
auto c = count<d>();
auto cOther = count<otherD>();
return c * cOther;
}
};
int main() {
Foo f(3,2);
std::cout << f.totalCount<Dir::U, Dir::V>() << std::endl;
}
但这似乎不是很优雅。
otherDir<d>()
不是constexpr
,不能在模板参数(count<otherDir<d>()>()
)中使用。
您可以向该方法添加constexpr
(和static
),或者使用旧结构来处理它:
template <Dir> struct otherDir;
template<>
struct otherDir<Dir::U>
{
static constexpr Dir value = Dir::V;
};
template<>
struct otherDir<Dir::V>
{
static constexpr Dir value = Dir::U;
};
演示
问题可能在这里:
auto cOther = count<otherDir<d>()>();
模板在编译时"解析"。您应该将constexpr
用于otherDir<d>()
,因为只是简单的const
并不意味着可以在编译时计算该方法。
更传统的方法是使用结构otherDir
而不是方法。创建结构模板,两个实例化将带有static const value = V
或U
。
或者,您可以将枚举替换为空结构。
或者,更好的是,您可以尝试在没有模板的情况下实现它,并为维度创建类,每个维度都有一个对象。
相关文章:
- .cpp和.h文件中的模板专用化声明
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 调用专用模板时出错"no matching function for call to [...]"
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 模板专用化(按容器):value_type
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 获取非专用标准::矢量容器的标准方法<bool>
- C++ 模板专用化:获取模板参数
- C++模板专用获取器
- 在对类方法执行指针部分专用化时获取"illegal use of explicit template arguments"