1

これがあれば、私は疑問に思っていました:

#define size 8
#if ( 0 < size ) and ( size <= 16 )
  static unsigned char value;
#elif ( 8 < size ) and  ( size <= 16 )
  static unsigned short value;
#elif ( 16 < size ) and  ( size <= 32 )
  static unsigned value;
#elif ( 32 < size ) and  ( size <= 64 )
  static unsigned long value;
#else
  return 0;
#endif
#undef size

定数で可能ですか?私はもう試した:

const unsigned char size = 8;
if ( ( 0 < size ) &&  ( size <= 8 ) ) {
  static unsigned char value;
} else if ( ( 8 < size ) &&  ( size <= 16 ) ) {
  static unsigned short value;
} else if ( ( 16 < size ) &&  ( size <= 32 ) ) {
  static unsigned value;
} else if ( ( 32 < size ) &&  ( size <= 64 ) ) {
  static unsigned long value;
} else {
  return 0;
}

しかし、結果として私は持っています:

致命的なエラー: 宣言されていない識別子 '値' の使用

これは可能ですか?

4

3 に答える 3

1

あなたが使用することができます

typedef boost::uint_t<16>::exact my_uint16_t;
typedef boost::uint_t<8>::exact  my_uint8_t;
// etc.

これは、コンパイル時の定数で機能します。

constexpr int mybitcount = 8;

void foo(boost::uint_t<mybitcount> ui8)
{
}

ブースト整数を参照してください

template<int Bits>
struct uint_t 
{
    /* Member exact may or may not be defined depending upon Bits */
    typedef implementation-defined-type  exact;
    typedef implementation-defined-type  least;
    typedef uint_fast_t<least>::fast      fast;
};
于 2013-07-29T19:18:05.037 に答える