简体   繁体   English

返回类型取决于参数数量的重载可变参数函数

[英]overload variadic function with return type dependent on number of parameters

I am writing a multi-dimensional array class for which the number of dimensions is not known until runtime. 我正在编写一个多维数组类,其维数直到运行时才知道。

I have gotten to the subscript operator and would like to replicate the behavior of a multidimensional native array so that: 我已经到达下标运算符,并且想要复制多维本机数组的行为,以便:

  • when using fewer subscripts than the number of dimensions, it will return an array (what might be called a "view") 当使用的下标少于尺寸数时,它将返回一个数组(可能称为“视图”)
  • when using a number of subscripts equal to the number of dimensions it will return the value stored at that location 当使用的下标数量等于尺寸数时,它将返回存储在该位置的值

Is it possible to have the return type depend on the relationship of the number of arguments passed to a function and a value that is not known until runtime (the number of dimensions)? 返回类型是否可能取决于传递给函数的参数数量与直到运行时才知道的值(维数)之间的关系?

Other info: 其他资讯:

  • I am implementing it as a sudo-multidimensional array by using a flat array and math to determine the correct index. 我通过使用平面数组和数学运算来确定正确的索引,将其实现为sudo多维数组。
  • I am writing the subscript operator as a variadic function (non-templated). 我将下标运算符写为可变参数函数(非模板)。

The requirements, 要求,

  • when using ... it will return an array 使用...时,它将返回一个数组
  • when using ... it will return the value 使用...时,它将返回值

contradict the standard (1.3.11) - a method or an operator cannot be overloaded on return types. 与标准(1.3.11)相矛盾-方法或运算符不能在返回类型上重载。 So essentially it will be impossible for you to have something simple like: 因此,从本质上讲,您不可能拥有以下简单的东西:

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) {...}
};

As noted in the comments you could return a boost::variant or your own object instead. 如注释中所述,您可以返回boost::variant或您自己的对象。 The variant is the better idea of course. 当然,变体是更好的主意。 The advantage of your own object is that you'll be able to control the conversions directly, but I don't consider this as a particularly good idea: 您自己的对象的优点是您可以直接控制转换,但是我不认为这是一个特别好的主意:

class Result; 
...
struct MyArrray {
   const Result& operator[] (const MyIndex& x) {...}
   const Result& operator[] (int, int, int) {...}

then add user defined conversions for Result or possibly separate getter operations to examine and retrieve values: 然后为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 */ }  

}; };

You can already note that this quickly becomes a quagmire. 您已经可以注意到,这很快就变成了泥潭。 It'll be hard to maintain, hard to reason about, and your code will not be explicit to the readers or reviewers. 这将很难维护,难以推理,并且您的代码对读者或审阅者而言也不会明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是一个c ++ 11可变参数函数模板重载与依赖类型不明确? - Is a c++11 variadic function template overload with a dependent type ambiguous? 如何使用可变数量的参数重载 lambda 函数? - How can I overload a lambda function with a variadic number of parameters? 在 function 返回类型中使用递归可变模板参数 - Using recursive variadic template parameters in function return type Function 模板重载解析,依赖和非依赖参数 - Function template overload resolution, dependent and non-dependent parameters 可变参数函数-确定返回类型 - Variadic function - determining return type 使用未知数量的参数调用可变参数函数 - Calling a variadic function with an unknown number of parameters 可变参数模板函数参数和引用类型推导 - Variadic template function parameters and reference type deduction 为什么在C ++中不可能重载返回类型,而可能重载参数? - Why is it is impossible to overload return type in C++, but possible to overload parameters? 如何使用相同的类型和参数重载调用 operator() function,但返回类型不同? - How to overload call operator() function with same type and parameters, but different return type? 确定参数的数量和类型以及作为函数的类型参数的返回类型 - Determining number and type of parameters and return type of type parameter that is a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM