【发布时间】:2021-12-30 05:43:30
【问题描述】:
以下程序:
#include <type_traits>
template<typename T, bool b>
struct S{
S() = default;
template<bool sfinae = true,
typename = std::enable_if_t<sfinae && !std::is_const<T>::value>>
operator S<T const, b>() { return S<T const, b>{}; }
};
template<typename T, bool b1, bool b2>
void f(S<const std::type_identity_t<T>, b1>,
// ^- T in non-deduced context for func-param #1
S<T, b2>)
// ^- T deduced from here
{}
int main() {
S<int, true> s1{};
S<int, false> s2{};
f(s1, s2);
}
被 GCC (11.2) 接受,但被 Clang (13) 和 MSVC (19.latest) 拒绝,全部用于 -std=c++20 / /std:c++20 (DEMO)。
- 这里什么编译器是正确的?
【问题讨论】:
-
我正在添加一个我认为可能是准确的自我回答,这是我在写问题时发现的,但我希望(至少!)第二意见。
-
bool x确实可以推导出来,但是x不能从S<const int, x>推导出S<int, true>
标签: c++ templates compiler-errors language-lawyer template-argument-deduction