c – SFINAE和noexcept说明符

功能模板的重载解析过程中,noexcept说明符括号中的表达式是否参与SFINAE?

我想为聚合创建一个包装器,并希望std :: is_constructible谓词正常工作:

template< typename type >
struct embrace
    : type
{

    template< typename ...arguments >
    embrace(arguments &&... _arguments) noexcept(noexcept(type{std::forward< arguments >(_arguments)...}))
        : type{std::forward< arguments >(_arguments)...} // braces 
    { ; }

};

int
main()
{
    struct S { int i; double j; }; // aggregate
    using E = embrace< S >;
    E b(1,1.0); // "parentheses"-constructible => can be used as usual types
    b.i = 1; b.j = 2.0; // accessible
    static_assert(std::is_constructible< E,int,double >{});
    static_assert(std::is_constructible< E,struct B >{}); // want hard error here
    return EXIT_SUCCESS;
}

但是我尝试在noexcept规范中使用noexcept操作符来启用SFINAE失败,并且模板构造函数接受传递给它的所有内容.构造函数如何被限制?

标准不允许专门从< type_traits&gt ;.的任何谓词.如何处理接受可变模板参数包和SFINAE的转换器?是否有僵局和固有的语言缺陷?

解决方法

SFINAE根本不适用于异常规范,无论是否是功能类型的一部分.

见[temp.deduct] / 7中的注释:

The substitution occurs in all types and expressions that are used in
the function type and in template parameter declarations. The
expressions include not only constant expressions such as those that
appear in array bounds or as nontype template arguments but also
general expressions (i.e.,non-constant expressions) inside sizeof,
decltype,and other contexts that allow non-constant expressions. The
substitution proceeds in lexical order and stops when a condition that
causes deduction to fail is encountered. [ Note: The equivalent
substitution in exception specifications is done only when the
exception-specification is instantiated,at which point a program is ill-formed if the substitution results in an invalid type or
expression. —end note ]

P0012R1 didn’t change anything在这方面.

Piotr的答案涵盖了您的代码的修复.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...