【发布时间】:2022-11-11 02:27:38
【问题描述】:
我在 enable_if'd 成员函数上调用 bind 时被绊倒了。我已经使用if constexpr 解决了这个问题,但我很好奇这个问题的解决方案是什么。该代码是最简单的复制器,并不代表我要解决的总体问题。我怎样才能使这项工作?
#include <iostream>
#include <type_traits>
#include <functional>
template <typename T>
class test {
public:
template <typename U = T>
typename std::enable_if<std::is_copy_constructible<U>::value, void>::type
foo(const T& v){
std::cout << "copy constructible";
}
template <typename U = T>
typename std::enable_if<!std::is_copy_constructible<U>::value, void>::type
foo(const T& v){
std::cout << "not copy constructible";
}
void foo_bar(const T& v){
std::cout << "test";
}
};
int main(int argn, char** argc){
test<int> myfoo;
myfoo.foo(3); //Works
auto func = std::bind(&test<int>::foo_bar, &myfoo, 3); //Works
auto func = std::bind(&test<int>::foo, &myfoo, 3); //Doesn't work
func();
return 0;
}
【问题讨论】:
-
为什么不使用 use lambda 代替:
auto func = [&myfoo]() { myfoo.foo(3); }? -
这是一个非常简化的低级库示例。绑定在代码中,我只有很小的控制权
标签: c++ metaprogramming sfinae