假设我们想使用c++的initializer_list来表示一些用户定义的类型,该类型表示预定义大小的数组(在编译时知道)。例如,矩阵的索引(行、列):
#include <initializer_list>
#include <algorithm>
#include <assert.h>
using idx_t = unsigned;
class MatrixIdx {
public:
MatrixIdx() :
_idx { 0, 0 }
{}
// this is how I'm able to use initializer list for now
// cons:
// * run-time assertion
// * additional header (algorithm)
// * not in member initializer list (call me perfectionist)
//
MatrixIdx(std::initializer_list<idx_t> l)
{
assert(l.size() == 2);
std::copy(l.begin(), l.end(), _idx);
}
//I want to achive constructor similiar to this one
//Won't compile
//
//MatrixIdx(std::initializer_list<idx_t> l) :
//static_assert(l.size() == 2, "two elements expected") //compile time assertion
//_idx[0] ( l[0] ),
//_idx[1] ( l[1] )
//{}
idx_t row() { return _idx[0]; }
void setRow(const idx_t i) { _idx[0] = i; }
idx_t col() { return _idx[1]; }
void setCol(const idx_t i) { _idx[1] = i; }
private:
idx_t _idx[2];
};
int main(void)
{
MatrixIdx idx; //default - OKAY
//MatrixIdx idxIL { 1, 2 }; //this should compile
//this should emit human-readable compile-time error
//MatrixIdx idxILError { 1, 2, 3 };
return 0;
}
实现这一目标的最佳方法是什么?
功劳应该归功于@IgorTandetnik,他提供了与op评论相同的答案。
对于您的情况,可以非常简单地采用统一初始化语法,只需提供一个接受两个类型为idx_t
的参数的构造函数。
class MatrixIdx {
public:
...
MatrixIdx() : ... {}
MatrixIdx(idx_t, idx_t) : ... {}
};
在主代码中:
int main()
{
MatrixIdx mat {}; // Okay
MatrixIdx mat {1,2}; // Okay
MatrixIdx mat {1,2,3}; // Error - no 3-arg. c'tor for MatrixIdx
}