【发布时间】:2020-06-11 21:50:58
【问题描述】:
我对以下程序中的 std::is_invocable 有疑问:
#include <iostream>
#include <type_traits>
void g() {}
template<bool B, typename T>
void f(T t) {
if constexpr (B)
g(t);
}
int main() {
std::cerr << std::boolalpha <<
std::is_invocable_v<decltype(&f<true, int>), int> << std::endl;
}
我本来希望程序输出为 false,因为 f
test.cpp: In instantiation of 'void f(T) [with bool B = true; T = int]':
test.cpp:14:51: required from here
test.cpp:9:10: error: too many arguments to function 'void g()'
9 | g(t);
| ~^~~
test.cpp:4:6: note: declared here
4 | void g() {}
|
和 Clang(从 11.0.0 开始)甚至打印 true。
在这种情况下正确的行为是什么?
【问题讨论】:
标签: c++ templates c++17 language-lawyer typetraits