【问题标题】:Template substitution fails when I change the type parameter name - compiler bug?更改类型参数名称时模板替换失败 - 编译器错误?
【发布时间】: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


    【解决方案1】:

    表达式 sfinae 在 msvc 2015 中不起作用。任何它起作用的情况都是偶然的,不要相信它。 decltype 不能用于可靠地导致 msvc 2015 中的 sfinae。

    停下来,走开,寻找另一个解决方案。也许尝试编译器内在函数。

    不要假设当参数名称匹配时意外工作意味着其他任何事情都可以工作,或者它会在稍微不同的程序中工作。

    您的解决方法不可信。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 2020-11-17
    • 1970-01-01
    • 2020-10-13
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多