所以我有以下测试代码:
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
which 尝试将其传递给other_test_func
我,则会收到一个错误,即返回的基础类型std::bind
无法转换为float
. 所以它试图将其转换为实际函数的返回值。我不明白为什么。我在哪里传递函数的返回值?
编辑
如果我在将局部变量设置为bind_empty_f
first 的值后调用该函数,它将编译:
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 或所有其他经过测试的编译器中的错误。
所以我想问题变成了,这个标准符合代码吗?