我有一个C++模板函数:
template<typename Input, typename Output>
void Process(const Input &in, Output *out) {
...
}set<int> sout; Process(vector<int>(), &sout);应该可以工作,但是vector<unsigned> vout; Process(vector<int>(), &vout);应该是编译错误。struct A {}; struct B {}; vector<B> vbout; Process(vector<A>(), &vbout);应该是编译错误。`发布于 2013-12-31 16:07:19
您只需确认这两种类型的static_assert() s是相同的:
static_assert(std::is_same<typename Input::value_type, typename Output::value_type>::value,
"the containers passed to Process() need to have the same value_type");如果您希望您的类型是可转换的,则可以使用std::is_convertible:
static_assert(std::is_convertible<typename Input::value_type, typename Output::value_type>::value,
"the value_type of the source container passed to Process() needs to be convertible to the value_type of the target container");https://stackoverflow.com/questions/20859950
复制相似问题