[英]Is a c++11 variadic function template overload with a dependent type ambiguous?
[英]overload variadic function with return type dependent on number of parameters
我正在编写一个多维数组类,其维数直到运行时才知道。
我已经到达下标运算符,并且想要复制多维本机数组的行为,以便:
返回类型是否可能取决于传递给函数的参数数量与直到运行时才知道的值(维数)之间的关系?
其他资讯:
要求,
与标准(1.3.11)相矛盾-方法或运算符不能在返回类型上重载。 因此,从本质上讲,您不可能拥有以下简单的东西:
class MyIndex; // defined elsewhere
template <typename T> struct MyArray {
const std::array<T>& operator[] (const MyIndex& x) {...}
const T& operator[] (const MyIndex& x) {...}
const T& operator[] (int, int, int) {...}
};
如注释中所述,您可以返回boost::variant
或您自己的对象。 当然,变体是更好的主意。 您自己的对象的优点是您可以直接控制转换,但是我不认为这是一个特别好的主意:
class Result;
...
struct MyArrray {
const Result& operator[] (const MyIndex& x) {...}
const Result& operator[] (int, int, int) {...}
然后为Result
添加用户定义的转换,或者可能添加单独的getter操作以检查和检索值:
class Result {
bool hasInt() const;
operator int() const { /* throw if wraps a view else return the value */ }
bool hasView() const;
operator MyArray() const { /* throw if wraps a value else return your array as a view */ }
};
您已经可以注意到,这很快就变成了泥潭。 这将很难维护,难以推理,并且您的代码对读者或审阅者而言也不会明确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.