在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< …Run Code Online (Sandbox Code Playgroud) 有没有语法可以约束非模板化方法?我在使用clang concept branch和gcc的Godbolt上尝试过的所有语法都无法编译:
// these examples do not compile
template <bool B>
struct X
{
requires B
void foo() {}
};
template <class T>
struct Y
{
requires (std::is_trivially_copyable_v<T>)
auto foo() {}
};
Run Code Online (Sandbox Code Playgroud)
使其编译的技巧与使用SFINAE所需的技巧相同,使方法成为模板,即使它们实际上不是模板。有趣的是,约束似乎不需要方法模板,它可以单独在类模板上正常工作,所以我真的希望有一种方法可以将约束应用于概念而不必求助于旧的技巧:
// old hacks
template <bool B>
struct X
{
template <bool = B>
requires B
auto foo() {}
};
template <class T>
struct Y
{
template <class = T>
requires std::is_trivially_copyable_v<T>
auto foo() {}
};
Run Code Online (Sandbox Code Playgroud)
现实生活中的例子:
template <class T, bool Copyable_buf = false>
struct Buffer
{ …Run Code Online (Sandbox Code Playgroud)