Ege
Ege

Reputation: 97

C++ define a static variable in if condition using g++

The following macro compiles in Vistual Studio C++ (not excatly as shown below):

#define LogMacro(ChannelID) \
        if(static ChannelSettingStruct* channel_settings = SingletonClass()->Instance()->GetSettings(ChannelID))
        ... use channel_settings to determine the settings of a channel/component and log accordingly...
        ... for example if logging for a channel is enabled or disabled ...
        ... if all the conditions are true then write log to file using the macro below. Just like std::cout it uses << operator to pump char to stream ...
        LogToFileMacro(channel ...)

The usage of the macro above:

LogMacro("ExampleChannel") << "Some text to log" ;
  1. channel ID is used to differentiate logs via channel, e.g. Channel1, Channel2
  2. The macro above works in Visual Studio.
  3. I use macros to reduce the number of function call.
  4. If I could I would definetly avoid macros. But I am forced to use macros.

The problem:

However the same macro raises the following error when I use the g++ compiler:

error: decl-specifier invalid in condition

That is because g++ does not allow static variable to be declared in if condition. The reason I have made the variable "channel_settings" static is because I thought it would lead to one copy per ChannelID. However I am not sure about this.

My aim is to define a pointer variable in a macro so that when I call the same macro again, the compiler does not raise an error due to redefinition of that variable. Just like in Visual Studio C++.

g++ does not allow this.

Is there any work around for the g++ compiler?

Upvotes: 0

Views: 1459

Answers (4)

ByteMe95
ByteMe95

Reputation: 876

You can acheive this with statement expressions

At least on gcc

if( ( { static ChannelSettingStruct* channel_settings = SingletonClass()->Instance()->GetSettings(ChannelID)); channel_settings; } ) )
{
    ...
}

Upvotes: 0

Ege
Ege

Reputation: 97

Answer to my questions, I am using g++ version 4.8.5,

if you declare a variable in a macro such as:

#define Macro1 \ if(int x = 5)

this macro can be called multiple times within same scope.

Declaring without the if condition will raise an error due to redefinition, if called more than once within same scope, such as:

#define Macro2 \ int x = 5; (compile error due to redefinition)

Unlike Visual Studio, g++ does not allow static variables to be declared in if condition. Thinking about it now, I am not sure if the following lines has any different meaning:

if( int x = 5 ) compared to if( staitc int x = 5 )

I believe their scope is just within the If condition. If anyone knows please tell me!

Upvotes: 0

SergeyA
SergeyA

Reputation: 62593

It is not making any sense. Even if macro would work this way (they don't, but even if they would) the code would not serve any purpose.

The point of declaring a variable inside if() condition is to make this variable accessible below. For example,

if (int rc = function())
    std::cout << "Function failed, rcode = " << rc << "\n"

would allow someone who only needs to know the function result code when it's non-0 prevent rc from polluting the scope. But in your case you say you do not need the variable. Since it is also makes no sense to code the check as stated at your question (if (x = 5) is always true) I assume you actually want to test the result of the function.

Taking this together, you simply need to do if (func())....

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 180805

My aim is to define a static variable in a macro so that when I call the same macro again, the compiler does not raise an error due to redefinition of that variable.

That is not how macros work. When you have a macro it does a direct text replacement of the line the macros is used with the contents of the macro. If you call the macro in several places then you will redefine the variable.

In C++ this can generally be solved using a function and declare a function local static variable.

Upvotes: 2

Related Questions