在函数声明和定义中使用 noexcept 说明符?

2023-12-28

考虑以下函数:

// Declaration in the .h file
class MyClass
{
    template <class T> void function(T&& x) const;
};

// Definition in the .cpp file
template <class T> void MyClass::function(T&& x) const;

我想做这个功能noexcept如果类型T不可构造。

怎么做 ? (我的意思是语法是什么?)


像这样:

#include <type_traits>

// Declaration in the .h file
class MyClass
{
    public:
    template <class T> void function(T&& x) noexcept(std::is_nothrow_constructible<T>::value);
};

// Definition in the .cpp file
template <class T> void MyClass::function(T&& x) noexcept(std::is_nothrow_constructible<T>::value);

实例 http://ideone.com/N3KqHV

但也请参阅为什么模板只能在头文件中实现? https://stackoverflow.com/q/495021/1782465。您(通常)无法在源文件中实现模板。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在函数声明和定义中使用 noexcept 说明符? 的相关文章

随机推荐