我想将绑定类模板包装到一个单独的命名空间中:
namespace my_space {
template<typename... R> using bind = std::bind<R...>;
}
并收到错误:
error: 'bind<R ...>' in namespace 'std' does not name a type.
我怎么能这样做?可以找到一个小例子here。
答案 0 :(得分:8)
您的代码无法编译,因为std::bind
是函数,而不是类型。您只能对类型使用using
声明别名。
虽然g++
诊断不是最好的,但Clang ++会given you出现以下错误:
错误:预期类型
更清楚*。
谢天谢地,你可以使用以下方法导入std::bind
名称:
namespace my_space {
using std::bind;
}
具体定义如下:
§7.3.3/ 1
using
声明[namespace.alias]using声明在声明区域中引入了一个名称,其中出现了using声明。
*个人意见。
答案 1 :(得分:3)
我不知道你是否可以使用它,但另一种选择可能是通过完美转发进行包装。任何好的编译器都会优化包装器。
namespace my_space {
template<class... Args>
auto bind(Args&&... args) -> decltype( std::bind(std::forward<Args>(args)...) )
{
return std::bind(std::forward<Args>(args)...);
}
}
在C ++ 14中,您甚至可以删除-> decltype( std::bind(std::forward<Args>(args)...) )
部分。
可以找到一个工作示例here
答案 2 :(得分:2)
如果你真的保留了原始模板参数,那么只需输入名称:
namespace my_space {
using std::bind;
}