template <typename T>
MyFun(const T container)
{
// I want to static_assert that all elements in T are equal to SomeType
}
我怎样才能做到这一点?我在想一些类似的东西,static_assert(std::is_same<T::type,SomeType>)
但这当然行不通......
template <typename T>
MyFun(const T container)
{
// I want to static_assert that all elements in T are equal to SomeType
}
我怎样才能做到这一点?我在想一些类似的东西,static_assert(std::is_same<T::type,SomeType>)
但这当然行不通......
你可以使用
static_assert(std::is_same<typename T::value_type,SomeType>::value, "type in the container is different");
如果是标准容器...
template <typename Container>
MyFun(const Constainer& container)
{
static_assert(std::is_same<typename Container::value_type, SomeType>::value)
// I want to static_assert that all elements in Container are equal to SomeType
}
你需要类似的东西
static_assert(std::is_same<typename T::value_type, SomeType>::value,
"It does not work");
假设定义的容器类型value_type
是它所拥有的元素的类型(就像标准库容器一样)。