【发布时间】:2016-10-25 10:22:47
【问题描述】:
我正在编写一个函数,其中我将map、set、unordered_map、unordered_set 的键复制到vector,现在我想添加一个编译时断言以清除错误,如果有的话尝试在该函数中传递vector、list。
template <typename container>
auto CopyKeyToVector(conatiner c)
{
//static assert to check c is map, unordered map only?
}
知道我们如何做到这一点 - 作为 map、unordered_map 本身的模板化容器
【问题讨论】:
-
如果你使用
c,因为它是一个关联容器,即只调用一个只存在于例如std::map或std::unordered_map,那么如果调用者传递了一些没有该成员函数的东西,你会得到一个编译器错误。
标签: c++11 c++14 compile-time static-assert