【发布时间】:2016-08-16 21:29:10
【问题描述】:
以下大部分代码取自 Piotr Skotnicki 的 answer。我正在试验它并发现了我认为是 MSVC 14.0 Update 3 中的一个错误。
考虑以下代码:
#include <iostream>
template <typename T>
struct identity { using type = T; };
template <typename...>
using void_t = void;
template <typename F>
struct call_operator;
template <typename C, typename R, typename... A>
struct call_operator<R(C::*)(A...)> : identity<R(A...)> {};
template <typename C, typename R, typename... A>
struct call_operator<R(C::*)(A...) const> : identity<R(A...)> {};
template <typename F>
using call_operator_t = typename call_operator<F>::type;
template <typename, typename = void_t<>>
struct is_convertible_to_function
: std::false_type {};
template <typename L>
struct is_convertible_to_function<L, void_t<decltype(&L::operator())>>
: std::is_assignable<call_operator_t<decltype(&L::operator())>*&, L> {};
template <typename, typename = void_t<>>
struct is_callable_object
: std::false_type {};
template <typename L>
struct is_callable_object<L, void_t<decltype(&L::operator())>>
: std::true_type {};
int main()
{
auto x = []() {};
std::cout << std::boolalpha << is_callable_object<decltype(x)>::value;
std::getchar();
}
这会按预期打印true,因为编译器生成的 lambda 对象确实实现了operator()。
现在让我们将is_callable_object 中的类型参数名称从L 更改为T(据我所见,任何与is_convertible_to_function 中使用的类型名称不同都会导致此问题)。
template <typename, typename = void_t<>>
struct is_callable_object
: std::false_type {};
template <typename T>
struct is_callable_object<T, void_t<decltype(&T::operator())>>
: std::true_type {};
突然,打印出false。 is_convertible_to_funtion 无关紧要,因为 is_callable_object 不以任何方式依赖它;事实上,如果我删除 is_convertible_to_function,这个问题就会消失 - 我可以使用任何我想要的类型名称。
正如我所说,我怀疑这是一个错误,所以我提出这个问题是为了确保这不是 C++ 标准中的一些奇怪行为;解决方法很简单。
【问题讨论】:
标签: c++ visual-studio templates template-meta-programming compiler-bug