可能的 g++ 错误与 noexcept 和模板
Possible g++ bug with noexcept and templates
当我
将模板与noexcept
说明符结合使用时,我收到有关不匹配的noexcept 规范的错误。它使用我使用过的各种版本的 clang 进行编译,并且在所有版本的 gcc 中都失败。
struct Y
{
void h();
};
template<typename T>
struct X
{
void f() noexcept(noexcept(std::declval<Y>().h()));
};
template<typename T>
void X<T>::f() noexcept(noexcept(std::declval<Y>().h()))
{
}
int main()
{
}
错误:
g++ -std=c++1y -O2 -Wall -pthread main.cpp && ./a.out
main.cpp:15:56: error: declaration of 'void X<T>::f() noexcept (noexcept (declval<Y>().Y::f()))' has a different exception specifier
void X<T>::f() noexcept(noexcept(std::declval<Y>().f()))
^
main.cpp:11:10: error: from previous declaration 'void X<T>::f() noexcept (noexcept (declval<Y>().Y::f()))'
void f() noexcept(noexcept(std::declval<Y>().f()));
^
这是一个错误吗?有什么办法可以绕过它吗?
使用枚举来存储 noexcept 运算符的结果至少在 ideone 当前使用的 gcc-4.9.2 中是一种可怕的解决方法。
#include <iostream>
#include <utility>
struct Y
{
void h() noexcept;
void i();
};
enum Y_noexcept_value
{
h = noexcept(std::declval<Y>().h()),
i = noexcept(std::declval<Y>().i())
};
template<typename T>
struct X
{
void f() noexcept(Y_noexcept_value::h);
void g() noexcept(Y_noexcept_value::i);
};
template<typename T>
void X<T>::f() noexcept(Y_noexcept_value::h)
{
}
template<typename T>
void X<T>::g() noexcept(Y_noexcept_value::i)
{
}
int main()
{
std::cout << std::boolalpha
<< noexcept(std::declval<X<int>>().f()) << std::endl
<< noexcept(std::declval<X<int>>().g()) << std::endl;
return 0;
}
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 'noexcept = default'编译错误
- 可能的 g++ 错误与 noexcept 和模板