在测试已声明但未定义的运算符是否存在时,static_assert真的应该成功吗?
Should static_assert really succeed when testing existence of an operator that's declared but not defined?
为什么下面的static_assert
成功?我只把<<
操作符作为朋友,但我在任何地方都没有创建它。
struct foo {
friend std::ostream &operator<<(std::ostream &stream, const foo &f);
};
template<typename T>
struct bar {
//succeeds:
static_assert(boost::has_left_shift<std::ostream,T>::value, "failure");
};
int main(int,char**) {
bar<foo> b;
return 0;
}
友元声明确定运算符存在。如果你使用它,编译器会接受它:
std::cout << foo();
这就是断言所能测试的全部内容。包含此语句的程序可能不会链接,因为您没有定义运算符,但就像编译器一样,断言无法检测到这一点。它不知道其他翻译单元是否最终会为该功能提供定义。
编译和链接是分开的阶段。
如果您在另一个文件中提供了定义,并编译了该文件,则可以将两个编译后的文件链接在一起,形成一个完整的程序。您不必重新编译第一个文件。这就是单独编译的全部内容。
相关文章:
- 在决定是通过参考还是通过价值时,尺寸真的是一个问题吗
- 字节真的是最小可寻址单元吗
- 如果我真的真的想从 STL 容器继承,并且我继承构造函数并删除新运算符,会发生什么?
- 如何在 std::vector 中找到<bool>哪些索引是真的?
- std::string 的对象真的可以移动吗?
- 在这种情况下,我真的复制了字节还是复制了字符?
- int8_t和uint8_t真的是整数吗?它们有什么用?
- 真的没有来自 std::string_view 的 std::string 的显式构造函数吗?
- 查找不等式为真的次数时出现问题
- 如何在 assert() 和 static_assert() 之间调度,如果在 constexpr 上下文中依赖?
- 考虑到其他好处,关键字'auto'真的有助于简化调试C++吗?
- 有没有更好的方法来处理异常? try-catch块真的很丑
- 在为嵌套类定义行外友元时,我真的必须打破封装吗?
- 我关于函数"Assert"的C++代码有问题
- 你如何理解"std: :forward is just syntactic sugar"?这是真的吗?
- "std::forward"和"std::move"真的不生成代码吗?
- VS 2017 和 2019 运行 c++ 真的很慢
- 结构和类在C++中真的等价吗?
- assert() 在发布模式下充当标识函数吗?
- 指向数组unique_ptr在调用 release() 后会自动释放动态内存,这是真的吗?