Compiler error occurred when try to use a flexible template expression in preprocessor definesCompil

S

snnn

Compiler error occurred when try to use a flexible template expression
in preprocessor defines

When I try to compile the following codes piece under Visual C++ .net
2003, I get an error "Fatal Error C1017"

typedef ::Loki::Typelist<int,::Loki::NullType> TList;
#if ::Loki::TL::Length<TList>::value
int x;
#endif

Fatal Error C1017:
invalid integer constant expression
The expression in an #if directive did not exist or did not evaluate to
a constant.

::Loki::TL::Length<TList> is a struct some like this:
struct Length{
enum { value=0}; // Indeed,the value should be the length of this
Typelist(TList)
}

emmm. try to the following one instead:
enum {value=::Loki::TL::Length<TList>::value};
#if value
int x;
#endif

Ok! No error, no warning. However, as you see, that's badly! I do not
need this extra enum value.

So, Help me, please! Where is the syntax error in the former case?

Thanks!
Thanks!
Thanks!
 
V

Victor Bazarov

snnn said:
Compiler error occurred when try to use a flexible template expression
in preprocessor defines

When I try to compile the following codes piece under Visual C++ .net
2003, I get an error "Fatal Error C1017"

typedef ::Loki::Typelist<int,::Loki::NullType> TList;
#if ::Loki::TL::Length<TList>::value

This is a preprocessor directive in which you are trying to use
a construct only understandable by the compiler. What are you trying
to do here?
int x;
#endif

Fatal Error C1017:
invalid integer constant expression
The expression in an #if directive did not exist or did not evaluate to
a constant.

::Loki::TL::Length<TList> is a struct some like this:
struct Length{
enum { value=0}; // Indeed,the value should be the length of this
Typelist(TList)
}

emmm. try to the following one instead:
enum {value=::Loki::TL::Length<TList>::value};
#if value

Since 'value' is not a defined macro, your program will not have the
following declaration, most likely. IOW, while it compiles, I don't think
it produces the desired effect.
int x;
#endif

Ok! No error, no warning. However, as you see, that's badly! I do not
need this extra enum value.

Neither does it work as you want it.

V
 
S

snnn

I want to expand the function declaration from a typelist such like
this:

#define DECLARE_FUNCTION_BY_TYPELIST_1(name,TList) \
void name:):Loki::TL::TypeAt<TList,0>::Result p);

so I can do that

typedef ::Loki::Typelist<int,::Loki::NullType> TList;
DECLARE_FUNCTION_BY_TYPELIST_1(func,TList)

then the only thing I need to do is declare
DECLARE_FUNCTION_BY_TYPELIST_2,DECLARE_FUNCTION_BY_TYPELIST_3,DECLARE_FUNCTION_BY_TYPELIST_4...

I want to declare a macro which can automatic decides which macro
should be invoke by calculating the length of that typelist.
 
V

Victor Bazarov

snnn said:
I want to expand the function declaration from a typelist such like
this:

#define DECLARE_FUNCTION_BY_TYPELIST_1(name,TList) \
void name:):Loki::TL::TypeAt<TList,0>::Result p);

so I can do that

typedef ::Loki::Typelist<int,::Loki::NullType> TList;
DECLARE_FUNCTION_BY_TYPELIST_1(func,TList)

then the only thing I need to do is declare
DECLARE_FUNCTION_BY_TYPELIST_2,DECLARE_FUNCTION_BY_TYPELIST_3,DECLARE_FUNCTION_BY_TYPELIST_4...

I want to declare a macro which can automatic decides which macro
should be invoke by calculating the length of that typelist.

Cannot be done. Preprocessor has finished expanding all macros by
the time the compiler gets to instantiated templates and generate
the length of the typelist. Research the possibility to branch in
the templates themselves instead of using macros.

V
 
S

snnn

No! I tried, it doesn't work.
value must defined as a macro but not a enum value.

:-(

any other solutions ?

How to expand the function declaration from a typelist?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top