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容器的基礎元素?
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容器的基礎元素?
你可以使用
static_assert(std::is_same<typename T::value_type,SomeType>::value, "type in the container is different");
幾乎正確。有一點點缺失。 – juanchopanza
@juanchopanza編輯,謝謝 –
如果它是一個標準集裝箱...
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
是它所擁有的元素的類型(如標準庫容器所做的那樣)。
它是如何「不起作用」?你會得到什麼錯誤? – juanchopanza