为什么重载运算符上的异常说明符'<<'不适用于任何 std::ostream 对象,但对库中定义的运算符不起作用?
Why exception specifiers on overloaded operators '<<' doesn't work to any std::ostream object, but does on those defined in the library?
exemplo.cpp:
#include <type_traits>
using std::is_same;
#include <utility>
using std::declval;
#include <iostream>
using std::ostream;
using std::cout;
struct Foo final {
int value;
inline constexpr Foo(const int i) noexcept : value{i} {};
inline ~Foo() = default;
};
ostream& operator<<(ostream& out, const Foo& foo) noexcept { return out << foo.value; }
int main() {
const Foo a(42);
static_assert(is_same<decltype(cout), ostream>::value == true, ""); // assert always to true...
static_assert(noexcept(cout << a) == true, ""); // assert to true if the operator on line 21 is defined noexcept(true)
static_assert(noexcept(declval<ostream>() << a) == true, ""); // assert always to false...
static_assert(noexcept(declval<decltype(cout)>() << a) == true, ""); // Same as line 32...
return 0;
}
编译命令:
g++ -std=c++2a -fconcepts exemplo.cpp -o exemp.run
错误:
exemplo.cpp:32:53: error: static assertion failed
static_assert( noexcept( declval<ostream>() << a) == true, ""); // assert always to false...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
exemplo.cpp:34:60: error: static assertion failed
static_assert( noexcept( declval<decltype(cout)>() << a) == true, ""); // same as line 32
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
'declval((' 函数"生成给定类型的伪编译时对象",即使这种类型不是真正可构造的。所以'declval(('应该产生另一个像std::cout这样的对象,其中第21行的重载运算符应该在编译时工作(但它没有(。
我意识到这也适用于 std::clog 和 std::cerr,两者都是 ostream 类型的变量。
它应该只是编译。我的意思是异常说明符应该与任何 ostream 对象相同,而不仅仅是这三个对象。
注意:使用 G++ 8.1.0 编译;不需要图像上的标志。实际上,没有标志或只有标志 -std=c++11 或更高版本可以给出相同的输出。
原因是
declval
生成一个临时对象,所以如果你的代码有另一个重载,像这样
ostream& operator<<(ostream&& out, const Foo& foo) noexcept { return out << foo.value; }
它会起作用。请注意,重载函数采用右值引用。我用 gcc 4.8.5 和 -std=c++11
对其进行了测试。
您不能将 prvalue ostream
绑定到ostream &
,而是应该有一个 ostream &
表达式。
int main() {
const Foo a(42);
static_assert(is_same<decltype(cout), ostream>::value == true, "");
static_assert(noexcept(cout << a) == true, "");
static_assert(noexcept(declval<ostream&>() << a) == true, "");
static_assert(noexcept(declval<decltype(cout)&>() << a) == true, "");
return 0;
}
现场观看
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何显式调用运算符<<
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 过载输出<<用于类的运算符,以打印其中的元组
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<:此运算符函数的参数太多
- C++继承运算符<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 为什么字符串流运算符<<擦除原始值
- 关于使用运算符<<为新手提供C++中的模板
- 我已经完成了<<运算符重载,但它'It’不起作用
- 重载运算符<<输出地址而不是数据成员
- 错误:没有匹配'运算符<<"在'std::cout
- 重载运算符<<用于ostream语法
- 当运算符<存在时,为什么要定义 LT?
- log4cxx访问异常,使用<<运算符和宏
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多