C++ SFINAE内部概念模板参数

C++ SFINAE内部概念模板参数,c++,sfinae,c++20,c++-concepts,C++,Sfinae,C++20,C++ Concepts,SFINAE在概念论证中起作用吗?(这里可能不叫SFINAE)。例如: template <class F> requires std::invocable<F, int> && // <-- is this needed? (!std::same_as<std::invoke_result_t<F, int>, void>) auto foo(F f, int a) -> int

SFINAE在概念论证中起作用吗?(这里可能不叫SFINAE)。例如:

template <class F>
    requires
        std::invocable<F, int> && // <-- is this needed?
        (!std::same_as<std::invoke_result_t<F, int>, void>)
auto foo(F f, int a) -> int
模板
要求
std::invocable&&//int
是否需要上述
std::invocable

如果我们这样省略它:

template <class F>
    requires (!std::same_as<std::invoke_result_t<F, int>, void>)
auto foo(F f, int a) -> int
模板
需要(!std::与相同)
自动foo(F F,int a)->int
即使
std::invoke\u result\t
不是(即,如果它不可开票),该版本是否格式正确,或者是UB/格式错误,ndr

foo(11, 24);
// std::invoke_result_t<int, int> does not exist,
// is the second variant the one without `std::invocable<F, int>` ok in this case?
foo(11,24);
//std::invoke_result_t不存在,
//在本例中,第二个变体是否没有“std::invocable”ok?


gcc似乎在没有它的情况下运行:

SFINAE仍然使用约束

替换失败会导致原子约束(例如
(!std::same_as)
)被视为不满足

[temp.Const.atomic]

要确定是否满足原子约束,请 参数映射和模板参数首先替换为 它的表情。如果替换导致无效的类型或 表达式,则约束不满足。否则 如有必要,进行左值到右值的转换,E应为 布尔类型的常量表达式。如果满足且 仅当E的计算结果为真时。如果,在 程序中,对同一个原子的满意度结果是不同的 约束和模板参数,程序格式不正确,没有 需要诊断。[示例:

template<typename T> concept C =
  sizeof(T) == 4 && !true;      // requires atomic constraints sizeof(T) == 4 and !true

template<typename T> struct S {
  constexpr operator bool() const { return true; }
};

template<typename T> requires (S<T>{})
void f(T);                      // #1
void f(int);                    // #2

void g() {
  f(0);                         // error: expression S<int>{} does not have type bool
}                               // while checking satisfaction of deduced arguments of #1;
                                // call is ill-formed even though #2 is a better match
模板概念C=
sizeof(T)==4&&!true;//需要原子约束sizeof(T)==4和!true
模板结构{
constexpr运算符bool()const{return true;}
};
模板需要(S{})
无效f(T);/#1
空f(int);/#2
void g(){
f(0);//错误:表达式S{}没有bool类型
}//检查#1的推导参数的满足性时;
//尽管#2是一个更好的匹配项,但call的格式不正确
-[结束示例]

在模板参数推断过程中,未满足的约束会导致推断过程不成功

[临时扣减]

。。。如果函数模板具有关联的约束 ([temp.Const.decl]),检查这些约束是否满足要求 ([临时施工])。如果不满足约束,请键入 扣除失败

在过载解决期间,由来已久的SFINAE规范仍然适用,因此,当替换失败时,不考虑过载

[temp.over]

。。。如果,对于给定的函数模板,参数推断失败 或者合成的函数模板专门化将是 格式错误,未将此类函数添加到候选函数集 该模板的函数

因此,总而言之,GCC的行为是正确的,正如人们所期望的那样