相关疑难解决方法(0)

为什么我不能在类中初始化非const静态成员或静态数组?

为什么我不能在类中初始化非const static成员或static数组?

class A
{
    static const int a = 3;
    static int b = 3;
    static const int c[2] = { 1, 2 };
    static int d[2] = { 1, 2 };
};

int main()
{
    A a;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器发出以下错误:

g++ main.cpp
main.cpp:4:17: error: ISO C++ forbids in-class initialization of non-const static member ‘b’
main.cpp:5:26: error: a brace-enclosed initializer is not allowed here before ‘{’ token
main.cpp:5:33: error: invalid in-class initialization of static data …
Run Code Online (Sandbox Code Playgroud)

c++ static const

93
推荐指数
3
解决办法
9万
查看次数

使用decltype/SFINAE检测操作员支持

(有些)过时的文章探讨了decltype与SFINAE一起使用的方法,以检测某种类型是否支持某些运算符,例如==<.

以下是检测类是否支持<运算符的示例代码:

template <class T>
struct supports_less_than
{
    static auto less_than_test(const T* t) -> decltype(*t < *t, char(0))
    { }

    static std::array<char, 2> less_than_test(...) { }

    static const bool value = (sizeof(less_than_test((T*)0)) == 1);
};

int main()
{
    std::cout << std::boolalpha << supports_less_than<std::string>::value << endl;
}
Run Code Online (Sandbox Code Playgroud)

这输出true,因为当然std::string支持<操作员.但是,如果我尝试将它与支持<运算符的类一起使用,我会收到编译器错误:

error: no match for ‘operator<’ in ‘* t < * t’
Run Code Online (Sandbox Code Playgroud)

所以SFINAE不在这里工作.我在GCC 4.4和GCC …

c++ decltype sfinae c++11

15
推荐指数
3
解决办法
6816
查看次数

这个声明在哪个编译器有效?

我一直在尝试使用此声明,但无论是在Visual Studio 2012还是CodeBlocks(使用GCC),它都不会构建.

(来自http://netghost.narod.ru/gff2/graphics/summary/fli.htm)

typedef struct _ColormapChunk
{
  CHUNKHEADER Header;        /* Header for this chunk */
  WORD NumberOfElements;     /* Number of color elements in map */
  struct _ColorElement       /* Color element (one per NumberOfElements) */
  {
   BYTE SkipCount;           /* Color index skip count */
   BYTE ColorCount;          /* Number of colors in this element */
   struct _ColorComponent    /* Color component (one /'ColorCount') */
   {
    BYTE Red;                /* Red component color */
    BYTE Green;              /* Green component color */
    BYTE …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
106
查看次数

标签 统计

c++ ×3

c++11 ×1

const ×1

decltype ×1

sfinae ×1

static ×1