返回无效引用
Returning an invalid reference
有时当我用C++编程时,我希望每个变量都有一个undefined
值,比如Javascript!。例如,当我为数组的越界元素返回值时,返回undefined
而不是抛出异常很有用,或者:
template <typename T, int SIZE>
class MyArray
{
T arr[SIZE];
static T badref;
public:
T &operator[](int i)
{
if (i >=0 && i < SIZE)
return arr[i];
else
throw std::string("OUT-OF-BOUNDS"); // or: return badref; !!
}
};
另一个肮脏(在我看来)选项是将预查找变量的引用作为错误引用变量返回。我知道我们不能将null
或类似的东西分配给引用变量。
是否有另一种格式良好的模式来返回引用,其中调用方能够找出返回的值无效?
编辑:我不是说pointer
你可以使用 boost::optional,正如他的评论中提到的@chris。它是Boost库的一部分。有关更多详细信息,请参阅此页面。
修改MyArray
类:
template <typename T, int SIZE>
class MyArray
{
T arr[SIZE];
public:
optional<T&> operator[](int i)
{
if (i >=0 && i < SIZE)
return optional<T&>(arr[i]);
else
return optional<T&>();
}
};
用法:
MyArray<int>() array;
// fill array with data
optional<int&> result = array[0];
if (result) {
// item was found
} else {
// index out of bounds
}
我希望每个变量都有一个未定义的值,比如 Javascript!
指针只有一个"未定义"值 (nullptr)。引用(根据定义)是指向有效实例的东西。
要返回对静态对象的引用,您应该在运算符的 const 值和非 const 值之间分开:
template <typename T, int SIZE>
class MyArray
{
T arr[SIZE];
static T badref;
public:
T &operator[](int i)
{
if (i >=0 && i < SIZE)
return arr[i];
else
// returning ref here would allow clients to write:
// MyArray<int> a;
// a[-1] = 5; // valid if you return a non-const reference
throw std::string("OUT-OF-BOUNDS");
}
const T &operator[](int i) const
{
if (i >=0 && i < SIZE)
return arr[i];
else {
// MyArray<int> a;
// a[-1] = 5; // will not compile (cannot assign to const)
static const T invalid = T();
return invalid;
}
}
};
无论您想到什么,您的解决方案都需要适合类型系统。因此,您的函数签名必须明确表示(以这种或那种方式)结果可能是 T,但也可能是其他东西。
常见的方法是:
- 不要返回值
,而是返回状态代码并通过"out"参数(指针或引用)输出值:
bool tryGet(int i, T& result);
返回一个元组(状态、值),如下所示:
std::tuple<bool, T> get(int i)
(如果无法获取,则认为第二个元组元素无关紧要 - 要求 T 具有默认构造函数)
使用
boost::variant
(灵活,但需要提升)- 使用
boost::optional
(上述的简化版本,当您只需要"要么T要么什么都不需要")
引用的主要目标是避免无效 (NULL) 值,同时允许函数修改其参数并防止复制数据。 如果需要 NULL 值,请使用指针。
相关文章:
- 从 BubbleSort* 类型的右值初始化 'AssortedSorter&' 类型的非常量引用无效"
- 为什么我总是收到此错误:从类型为"农场动物"的右值初始化类型的非常量引用无效|
- C++ 解决方法:"从类型"B*"的表达式初始化类型"C*&"的引用无效"
- 从类型 'Matrix&' 的右值初始化 类型的非 const 引用无效 'Matrix'
- 我可以在 std::list 中移动元素而不会使迭代器或引用无效,但是如何移动呢?
- 如何引用无效的输入,然后重新输入和如果其他语句
- 将 std::vector 收缩以适应对向量本身的引用无效
- C++:从类型"int*"的表达式初始化类型"const int*&"的引用无效
- 从类型为"矩阵"的右值初始化类型为"矩阵"的非常量引用无效
- 在 GASM 内联中对参数"N"的引用无效
- 从右值初始化非常量引用无效
- 错误:从类型"testGetter* const"的表达式初始化类型"const testGetter*&"的引用无效
- 尝试使用 uint*& 作为常量单元*&失败:从类型"uint8_t"的表达式初始化类型"const uint8_t*&"的引用无效
- 为什么会崩溃?对函数中静态变量的c++引用无效
- 在自定义迭代器上应用reverse_iterator后引用无效
- 错误:从类型"const int"的表达式初始化类型"int"的引用无效
- find_if错误:从类型为"node*"的表达式初始化类型"const node&"的引用无效
- 从类型 'YAML::Node&' 的右值初始化 类型的非常量引用无效 'YAML::Node'
- 从类型为"foo*"的右值初始化类型为"foo*&"的非常量引用无效?
- 取消引用无效指针但不使用结果是否是C++中的未定义行为