preprocessor define with #pragma directive [duplicate] - c++

This question already has answers here:
Pragma in define macro
(4 answers)
Closed 4 months ago.
I would like to do a #define which contains a #pragma directive but I got the following error.
Any idea?
#define FunctionPar_Begin typedef struct fpar { #pragma pack(4)
error C2121: '#': invalid character: possibly the result of a macro expansion

The one you probably want:
#define FunctionPar_Begin typedef struct fpar { _Pragma("pack(4)")
MSVC also allows for:
#define FunctionPar_Begin typedef struct fpar { __pragma(pack(4))
Clang/gcc also allow:
#define FunctionPar_Begin typedef struct fpar { __attribute__((packed, aligned(4)))

Related

_ADDRESSOF(v) macro definition in VS2019 C++ [duplicate]

This question already has answers here:
Implementation of addressof
(4 answers)
Why and when is cast to char volatile& needed?
(2 answers)
Closed last year.
In <vadefs.h> you'll find the following definition for the macro _ADDRESSOF(v) in VS2019:
#ifdef __cplusplus
#define _ADDRESSOF(v) (&const_cast<char&>(reinterpret_cast<const volatile char&>(v)))
#else
#define _ADDRESSOF(v) (&(v))
#endif
I'd like to understand the first definition above, when __cplusplus is defined. Why does it work?

Problem including base header in all child headers [duplicate]

This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 4 years ago.
I have a base class, which is declared in the following header file:
#pragma once
#include "StateHandler.hpp"
namespace ta {
class GameState {
public:
// ...
};
} /* ta */
Then, I have two children, which look like this:
#pragma once
#include "../GameState.hpp"
namespace ta {
class DefaultState: public ta::GameState {
public:
// ...
};
} /* ta */
and
#pragma once
#include "../GameState.hpp"
namespace ta {
class WorldMapState: public ta::GameState {
public:
// ...
};
} /* ta */
When trying to compile that, I get errors, that GameState was not declared in the second of the two children I include. When I remove the #pragma once from the GameState.hpp, it says that GameSate gets redefined. I get why that happens, but I can't find a way to fix that.
UPDATE: Using include-guards doesn't work either. I get the following error, when using #pragma once or include-guards:
In file included from /[...]/include/statemachine/gamestates.hpp:2,
from /[...]/include/common.hpp:4,
from /[...]/include/statemachine/gamestates/../StateHandler.hpp:5,
from /[...]/include/statemachine/gamestates/../GameState.hpp:4,
from /[...]/include/statemachine/gamestates/DefaultState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:1,
from /[...]/include/common.hpp:4,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/gamestates/WorldMapState.hpp:7:47: error: expected class-name before '{' token
class WorldMapState: public ta::GameState {
^
And this is the error I get, when I don't use include-guards or #pragma once in the GameState.hpp (this error appears 3 times):
In file included from /[...]/include/common.hpp:5,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/GameState.hpp:4:11: error: redefinition of 'class ta::GameState'
class GameState {
^~~~~~~~~
In file included from /[...]/include/statemachine/gamestates/WorldMapState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:2,
from /[...]/include/common.hpp:4,
from /[...]/include/statemachine/gamestates/../StateHandler.hpp:5,
from /[...]/include/statemachine/gamestates/../GameState.hpp:1,
from /[...]/include/statemachine/gamestates/DefaultState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:1,
from /[...]/include/common.hpp:4,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/gamestates/../GameState.hpp:4:11: note: previous definition of 'class ta::GameState'
class GameState {
According to this answer #pragma once has unfixable bugs. It should never be used.
You can use header guards like below
#ifndef HEADER_H
#define HEADER_H
// Header file code
#endif
#pragma once is not a standard even it's supported by many compilers should not used to guard header files instead use #ifndef. As there is no standard behavior for #pragma once, you shouldn't assume that the behavior will be the same on all compiler.

What is the difference between #ifdef VALUE vs #if defined (VALUE) [duplicate]

This question already has answers here:
Is #if defined MACRO equivalent to #ifdef MACRO?
(5 answers)
Closed 4 years ago.
#include<stdio.h>
#define MAX 0
int main()
{
#ifdef MAX
printf("MAX defined");
#endif
#if defined (MAX)
printf("MAX is defined");
#endif
return 0;
}
Both the #ifdef and #if defined give the same effect then what is the difference between them? I have not seen the disassembly code of these directives if you have seen then kindly try to explain that as well.
The difference is historical. Originally there was only #ifdef. The newer syntax is more flexible and allows combining tests with logical conditions, but in the simple form you can use them interchangeably.

initialize a typedef struct variable in CPP

I'm trying to access a 3rd party library in which one of the header contains a struct as follows
#if defined(V1) || defined(V3)
typedef struct
{
int8 ErrorCode;
boolean isValid;
} validation, *validation_p;
#endif // #ifdef V1/V3
So if i would like to access validation/validation_p how can I access within my cpp, It's very clear that we need to define V1/V3 but where and how do I define those??
Sorry for the basic question on CPP
To define a macro in the C preprocessor, use the syntax #define MACRO VALUE, or, for an empty flag macro, just #define MACRO. So, in your case, your code should like like:
#define V3 // or V1
#include <thirdpartylib>

Header Files Including each other [duplicate]

This question already has answers here:
Cyclic dependency between header files
(4 answers)
Closed 9 years ago.
I have two header files. decimal.h and integer.h each containing their respective classes.
I want to write something like this.
//integer.h
#ifndef INTEGER_H
#define INTEGER_H
#include "decimal.h"
class Integer
{
...
operator Decimal();
}
#endif
//decimal.h
#ifndef DECIMAL_H
#define DECIMAL_H
#include "integer.h"
class Decimal
{
...
operator Integer();
}
#endif
What is giving me the trouble is that since they are including each over it behaves strangely in Visual Studio and an generate strange compiler errors. Is there any way around this?
Maybe you want just a forward declaration?
// In Integer.h
class Decimal;
class Integer
{
...
operator Decimal();
};
(You missed the last semicolon in your code, by the way.)