Boost静态断言类型比较 [英] Boost Static Assertion for Type Comparision

查看:179
本文介绍了Boost静态断言类型比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题给我的编译器错误,我不知道如何正确写入

  struct FalseType {enum {value = false}; }; 
struct TrueType {enum {value = true}; };


template< typename T1,typename T2>
struct IsSame
{
typedef typename FalseType Result;
};


template< typename T>
struct IsSame< T,T>
{
typedef typename TrueType Result;
};

BOOST_STATIC_ASSERT((IsSame< Foo :: FooClass1,Foo :: FooClass1> :: Result :: value));

这个静态断言在使用时不应该失败,但不知何故CUDA的编译器NVCC给我以下错误:

 错误C2338:(IsSame< Foo :: FooClass1,Foo :: FooClass1> :: Result :: value)

我不知道该怎么办,所有其他STATIC ASSERTIONS工作,但类型比较不,在上面?错字?



任何想法?

我可以在NVCC之外工作吗?



<

它看到MSVC(由NVCC路由到)也有它的问题以及上面的版本.... hm ...



============= EDIT ======================
这里



这个剪辑应该在MSVC中编译,但它不会,所以我认为编译器错误:



错误C2118:负下标(WHHHHHYYYYYY)strange ....

  #include< iostream> 


使用namespace std;


struct FalseType {static const bool value = false; };
struct TrueType {static const bool value = true; };


template< typename T1,typename T2>
struct IsSame
{
typedef :: FalseType Result;
static const bool result = false;
};


template< typename T>
struct IsSame< T,T>
{
typedef :: TrueType Result;
static const bool result = true;
};

namespace OtherType {
struct Type1 {};
};

template<类型名_T> //从下面的设置
struct设置{
typedef _T myT;
typedef char static_assert_failed [((IsSame< _T,OtherType :: Type1> :: Result :: value))? 1:-1]; // USE HERE only :: result works,(BUT WHY)
};

int main(){

cout<< (IsSame< OtherType :: Type1,OtherType :: Type1> :: Result :: value)<< endl

}


解决方案

  typedef typename FalseType Result; 
typedef typename TrueType Result;

这是错误的,因为 FalseType code> TrueType 不是从属名称,因此 typename 在此处是非法的。



应为

  typedef FalseType结果; 
typedef TrueType结果;


$ b

似乎

  IsSame< _T,OtherType :: Type1> :: Result :: value 

事情是

  IsSame< _T,OtherType :: Type1> :: Result 

必须符合

 (typename IsSame < _T,OtherType :: Type1> :: Result):: value 

  typedef typename IsSame <_T,OtherType :: Type1> :: Result RealResult; 
typedef char static_assert_failed [RealResult :: value? 1:-1];

HTH。


The following problem gives me compiler errors and I am not sure how to write it correctly

struct FalseType { enum { value = false }; };
struct TrueType { enum { value = true }; };


template <typename T1, typename T2>
struct IsSame
{
typedef typename FalseType Result;
};


template <typename T>
struct IsSame<T,T>
{
typedef typename TrueType Result;
};

BOOST_STATIC_ASSERT( (IsSame< Foo::FooClass1 , Foo::FooClass1 >::Result::value) );

This static assertion should not fail when used, but somehow the compiler NVCC from CUDA gives me the following error:

error C2338: (IsSame< Foo::FooClass1 , Foo::FooClass1 >::Result::value)

I dont know what to do, all other STATIC ASSERTIONS work but the type comparision does not , what is wrong up there? Typo? Brackets?

I cant get my type comparision to work under NVCC?

Any ideas?

IT SEEMS THAT MSVC (which is routed to by NVCC) has its problems as well with the above version.... hm...

============= EDIT ======================== HERE A SNIPPET WHICH DOES NOT WORK IN MSVC!

This snipped should compile in MSVC, but it does not, so I assume compiler bug:

error C2118: negative subscript ( WHHHHHYYYYYY) strange....

#include <iostream>


using namespace std;


struct FalseType { static const bool  value = false ; };
struct TrueType {  static const bool  value = true ; };


template <typename T1, typename T2>
struct IsSame
{
  typedef ::FalseType Result;
  static const bool result = false;
};


template <typename T>
struct IsSame<T,T>
{
typedef ::TrueType Result;
static const bool result = true;
};

namespace OtherType{
   struct Type1{};
};

template< typename _T> // Settings from below
struct Settings{
   typedef _T myT;
   typedef char static_assert_failed[ ((IsSame< _T,OtherType::Type1>::Result::value)) ? 1 : -1 ]; // USE HERE only ::result works, (BUT WHY)
};

int main(){

   cout << (IsSame<OtherType::Type1,OtherType::Type1>::Result::value)<< endl;

}

解决方案

typedef typename FalseType Result;
typedef typename  TrueType Result;

This is wrong, because FalseType and TrueType aren't dependent names and therefore typename is illegal here.

It should be

typedef FalseType Result;
typedef  TrueType Result;

Update

It seems that

IsSame < _T, OtherType::Type1 >::Result::value 

is illegal. The thing is that

IsSame < _T, OtherType::Type1 >::Result 

must be qualified by typename but syntactically it is impossible, that is, the following is illegal, too

(typename IsSame <_T, OtherType::Type1 >::Result)::value 

I found the following solution which makes it work.

typedef typename IsSame <_T, OtherType::Type1 >::Result RealResult;
typedef char static_assert_failed[RealResult::value ? 1 : -1];

HTH.

这篇关于Boost静态断言类型比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆