我观看了Walter Brown在Cppcon14上关于现代模板编程(第一部分,第二部分)的演讲,他在演讲中展示了他的void_tSFINAE技术.
示例:
给定一个简单的变量模板,该模板计算void所有模板参数是否格式正确:
template< class ... > using void_t = void;
Run Code Online (Sandbox Code Playgroud)
以及检查是否存在名为member的成员变量的以下特征:
template< class , class = void >
struct has_member : std::false_type
{ };
// specialized as has_member< T , void > or discarded (sfinae)
template< class T >
struct has_member< T , void_t< decltype( T::member ) > > : std::true_type
{ };
Run Code Online (Sandbox Code Playgroud)
我试图理解为什么以及如何运作.因此一个小例子:
class A {
public:
int member;
};
class B {
};
static_assert( has_member< A …Run Code Online (Sandbox Code Playgroud) 我正在编写一个用于处理多项式的小型库。
\n主类称为poly,它包含 4 个重载的构造函数。
类的对象poly是一个多项式的表示。
完整代码可以在这里查看: https: //github.com/IQ8QI/polynomial-lib
\n在./test-poly/test-all.cpp我试图创建一个类的对象poly:
poly::poly my_poly = poly::poly((double)6.0, -2.0, 4.0);\nRun Code Online (Sandbox Code Playgroud)\n类的构造函数poly有:
//Create polynomial using just numbers\npoly(double values...);\n\n//Create polynomial using vector<double>\npoly(vector<double> values);\n\n//Create polynomial with non-zero base\npoly(int base, double values...);\n\n//Create polynomial with non-zero base and vector\npoly(int base, vector<double> values);\nRun Code Online (Sandbox Code Playgroud)\n不幸的是,我收到一个编译错误:
\n./test-all.cpp:20:63: error: call of overloaded \xe2\x80\x98poly(double, double, double)\xe2\x80\x99 is ambiguous\n 20 | poly::poly my_poly = poly::poly((double)6.0, -2.0, 4.0);\n …Run Code Online (Sandbox Code Playgroud)