-2
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>)但这当然行不通......

4

3 回答 3

2

你可以使用

 static_assert(std::is_same<typename T::value_type,SomeType>::value, "type in the container is different");
于 2013-10-15T05:12:33.470 回答
1

如果是标准容器...

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
}
于 2013-10-15T05:13:23.037 回答
1

你需要类似的东西

static_assert(std::is_same<typename T::value_type, SomeType>::value, 
              "It does not work");

假设定义的容器类型value_type是它所拥有的元素的类型(就像标准库容器一样)。

std::is_samestatic_assert

于 2013-10-15T05:13:33.023 回答