c++ - noexcept 运算符和 enable_if_t : do they work together?

标签 c++ gcc clang language-lawyer noexcept

考虑以下类:

struct S {
    template<typename T>
    std::enable_if_t<std::is_void<T>::value>
    f() noexcept {}

    template<typename T>
    std::enable_if_t<not std::is_void<T>::value>
    g() noexcept {}
};

正如预期的那样,编译:

s.f<void>();

这个不是:

s.g<void>();

令我困惑的是,下面的 main 是用 GCC (6.2) 编译的,而不是用 clang (3.9) 编译的:

int main() {
    static_assert(noexcept(&S::f<void>), "!");
    static_assert(noexcept(&S::g<void>), "!");
}

我会说第二个断言失败是因为特化无效。两个编译器不同意这一点。

哪个是正确的?

最佳答案

[except.spec]/13 :

The set of potential exceptions of an expression e is empty if e is a core constant expression (5.20).

也就是说,GCC 甚至不解析template-id,因为它从一开始就知道结果是true。 (因为 g<void> 不是静态数据成员模板特化,其类型重载了 operator& )。虽然很聪明,但这种行为是不符合规范的,因为任何 template-id 的出现都需要将参数替换到函数模板的声明中。

关于c++ - noexcept 运算符和 enable_if_t : do they work together?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141702/

相关文章:

c++ - OPENCV:image_proc 中的 PCA 应用程序错误

c - 如何使 scanf 读取超过 4095 个字符作为输入?

c++ - 使用 arm-none-eabi 进行 Clang 交叉编译

无法为 enp4s0 上的套接字设置 SO_BINDTODEVICE

c++ - 在 Clang 中,编写自定义 ASTMatcher 时可以访问 SourceManager 吗?

c++ - 使用 STL 类而不包含其适当的 header

C++如何使用按值传递和按引用传递重载方法

c++ - 如何在 C++ 中表示二维矩阵

c++ - 使用 _GLIBCXX_CXX11_ABI 来使用具有 C++ 11/14 功能的 pre-5.1 C++ ABI 有什么影响?

c++ - 何时对 GCC 使用 -g 标志