Template typedef includes char [] [] - works with VS2008, but not gcc

I have a library code I'm working with. It compiles and works in Visual Studio (2008), but not GCC (v4.8.4.)

In the header we have:

extern const char menu_styles[MENU_COUNT][MAX_LEN];

typedef SysEnum <s_type_t, c_long, no_style, un_style, MAX_LEN, (char *)&menu_styles> MenuStyleEnum;

Where is SysEnum (defined in another file):

template<class ETYPE, class BTYPE, int MINV, int MAXV, int MLEN, char* pStr> class SysEnum

And gcc borks with an error:

error: β€˜menu_styles’ cannot appear in a constant-expression

With which I completely agree. (Also, he's const char *distinguished by char *).

I expect VS2008 to just compile this typedef with const char *instead menu_styles, but I'm really not sure.

I fear that VisualSudio is adding some kind of constructor code so that this one char *actually points menu_styleswhenever this typedef is used.

What can I change when compiling with GCC?

+4
3

, .

typedef. ++ ( ) . , VS2008 . , , .

GCC , .

VS2008 GCC , VS2008.

, , ( ( )), char *pStr . pStr , SysEnum.

char [] [] , pStr . , MENU_STYLES_CODE, menu_styles. typedefs .

typedef SysEnum <s_type_t, c_long, no_style, un_style, MAX_LEN, MENU_STYLES_CODE> MenuStyleEnum;
0

extern const char menu_styles[MENU_COUNT][MAX_LEN];

template<const char* pStr> class SysEnum{
  ...
};

typedef SysEnum <???> MenuStyleEnum;

const char*, . , ??? .

char* char[] -, char*, :

extern const char menu_style0[MAX_LEN];
typedef SysEnum <menu_style0> MenuStyleEnum;//compiles...

, , :

typedef SysEnum <menu_styles[0]> MenuStyleEnum;
typedef SysEnum <*menu_styles> MenuStyleEnum;
typedef SysEnum <static_cast<const char *>(&menu_styles)> MenuStyleEnum;

, , menu_styles[0] ( VS ). , , .

. SysEnum , pStr ( cout<<pStr), ( pStr==otherCharPointer), :

#define _MENU_STYLE0_ "STYLE0" 
...

extern const char menu_style0[MAX_LEN]= _MENU_STYLE0_;
...

extern const char menu_styles[MENU_COUNT][MAX_LEN]={ _MENU_STYLE0_,  _MENU_STYLE1_, ...};
...

typedef SysEnum <menu_style0> MenuStyleEnum;//compiles!

, menu_styleX menu_styles, .

menu_styles, :

template<size_t Index> class SysEnum{
    //use menu_styles[index] for pStr
};

typedef SysEnum <0> MenuStyleEnum;

, menu_styles, SysEnum.

+1

There is no way to make sense of the pattern SysEnum. You need to understand how the parameter is used menu_styles.

And then you need to fix the library code in a smart way, for example.

constexpr const char menu_styles[] = "sample";
template <class ETYPE,
          class BTYPE,
          int MINV,
          int MAXV,
          int MLEN,
          const char* pStr>
class SysEnum;
typedef SysEnum<s_type_t, c_long, no_style, un_style, MAX_LEN, menu_styles>
    MenuStyleEnum;
0
source

Source: https://habr.com/ru/post/1625435/


All Articles