首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >static_assert可以检查一个类型是否是向量吗?

static_assert可以检查一个类型是否是向量吗?
EN

Stack Overflow用户
提问于 2011-08-05 17:12:09
回答 2查看 8K关注 0票数 21

static_assert可以检查一个类型是否是向量吗?也就是说,int会提出断言,而vector<int>不会。

我在想一些类似的东西:

代码语言:javascript
复制
static_assert(decltype(T) == std::vector, "Some error")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-05 17:19:08

是。考虑下面的meta函数:

代码语言:javascript
复制
#include <stdio.h>
#include <vector>

template <class N>
struct is_vector { static const int value = 0; };

template <class N, class A>
struct is_vector<std::vector<N, A> > { static const int value = 1; };

int main()
{
   printf("is_vector<int>: %d\n", is_vector<int>::value);
   printf("is_vector<vector<int> >: %d\n", is_vector<std::vector<int> >::value);
}

只需将其用作static_assert中的表达式即可。

票数 20
EN

Stack Overflow用户

发布于 2011-08-06 14:59:51

c++0x:

代码语言:javascript
复制
static_assert(std::is_same<T, std::vector<int>>::value, "Some Error");
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6953969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档