模板被推断为模板变量的错误类型

template being deduced as wrong type with template variable

所以我有以下测试代码:

struct empty_value{
    template<typename T>
    T as(){ return T(0); }
};

template<typename T, typename U, typename F>
auto empty_func(empty_value lhs, empty_value rhs, F f) -> decltype(f(lhs.as<T>(), rhs.as<U>())){
    return f(lhs.as<T>(), rhs.as<U>());
}

template<typename T, typename U, template<typename, typename> class F>
static auto bind_empty_f = std::bind(empty_func<T, U, F<T, U>>, std::placeholders::_1, std::placeholders::_2, F<T, U>{});

template<typename F>
void other_test_func(F&&){}

template<typename T, typename U, template<typename, typename> class F>
void test_func(){
    other_test_func(bind_empty_f<T, U, F>);
}

template<typename T, typename U>
struct my_add{
    decltype(auto) operator()(T lhs, U rhs){ return lhs + rhs; }
};

int main(){
    test_func<float, int, my_add>();
}

这源自我实际从事的工作。问题出现在bind_empty_f行。但只有当它传递给other_test_func时。当我尝试将它分配给这样的常规变量时:

int main(){
    auto var = bind_empty_f<float, int, my_add>;
}

一切都很愉快。但是如果我调用 test_func 试图将它传递给 other_test_func 我会得到一个错误,即 std::bind 编辑的基础类型 return 无法转换为 float.所以它试图将它转换为实际函数的 return 值。我不明白为什么。我在哪里传递函数的 return 值?


编辑

如果我在将局部变量设置为 bind_empty_f 的值后调用该函数,首先它会编译:

int main(){
    auto var = bind_empty_f<float, int, my_add>;
    test_func<float, int, my_add>;
}

所以问题一定是与静态初始化有关编译器错误。

编辑2

如评论中所述,这个确切的示例可以与其他编译器一起编译,但不能与原始测试的编译器 (GCC 5.2.0) 一起编译。

这是 GCC 5.2 或所有其他经过测试的编译器中的错误。

所以我想问题就变成了,这是符合标准的代码吗?

这是您的问题的一个最小示例:

template<class T> struct tag {};

template<typename T>
static auto bind_empty_f = tag<T>{};

template<typename T>
decltype(bind_empty_f<T>) test_func(){
  return 3.14f;
}

然后我们简单地 test_func<float>() 并且它 returns 3.14f。如果我们 test_func<int>() 它 returns 3.

如果我们首先执行 bind_empty_f<float>,而不是 test_func<float> 会产生错误。

在另一个模板中调用时为 bind_empty_f<T> 推导的类型设置为 T 而不是表达式右侧的类型。

如果直接调用,还没有计算出类型(好像有缓存),然后推导出正确的类型,我的test_func 构建失败(因为它试图将 3.14f 转换为 bind 表达式类型但失败了)。

这肯定是编译器的问题。您可以通过将 bind_empty_f 中的 auto 替换为 std::decay_t<decltype(stuff_on_rhs)>.

来解决此问题

请注意,您的一些绑定表达式还有其他问题,但它们不是此问题的核心。

live example compiling (wrongly), live example not compiling (correctly).