Any work around to make the below code work? it currently give the following error:
error: too many arguments provided to function-like macro invocation
X({1,2,3});
^
code:
#include <stdio.h>
#define X(a) a,
int main()
{
mystruct = X({1,2,3}));
}
I tried something with templates but so far I know (I'm a bit new to C++) templates aren't "powerful" enough to implement something like the X macro. As you may already noticied, what I'm looking for is implement something like the X macro. Others ideas to do this are very welcome too as long as everything is know at compile-time.
This does the trick:
#define X(...) __VA_ARGS__
struct mystruct {
int a,b,c;
};
int main() {
mystruct s = X({1,2,3});
}
Or as a variation:
#define X(...) {__VA_ARGS__}
struct mystruct {
int a,b,c;
};
int main() {
mystruct s = X(1,2,3);
}
Related
for a demo code
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef struct Student
{
public:
Student(){}
~Student(){}
static void print(int a,int b){printf("age is a\n");}
}Student;
int main(){
void (*p)(int, int) = &Student::print;
vector<void(*)(int,int)> tt;
tt.push_back(p);
tt[0](1,1);
return 0;
}
when I want to make the void(*)(int,int) as a struct member, like
struct void_func_st{
void(*)(int,int) f;
int a;
};
the code is wrong. I don't know whether the struct could be made actually as I'm not familiar with how the void(*)(...) works. Or I just didn't get the right way to make void(*)(...) as a struct member. Can anyone give some advice?
It would be (as you do for local variable p in main)
void(*f)(int,int);
As note, typedef/using or some "wrapper" might help to have more regular syntax, for example:
using bin_func = void(int, int);
bin_func* f2;
std::add_pointer_t<void(int, int)> f3;
std::type_identity_t<void(int, int)>* f4;
Use std::function and forget about the confusing function pointer syntax:
struct void_func_st {
std::function<void(int,int)>; f;
int a;
};
Even better, introduce a nice alias for it:
using MyFunction = std::function<void(int,int)>;
How to pass enum in a scope to another as function argument? As this is failing:
enum class L;
struct TestClass
{
void foo(L n)
{
int v = static_cast<int>(n);
int r[v] = { 9 };
cout << "\n" << v << "\n";
}
};
int main()
{
enum class L : int
{
A, B, C, D
};
TestClass main;
main.foo(L::D);
return 0;
}
error: cannot convert ‘main()::L’ to ‘L’
80 | main.foo(L::D);
| ~~~^
| |
| main()::L
How to solve this (in exact place, not move enum to a scope else) ?
How to solve this (in the exact place, not move enum to a scope else)?
Cast the enum while passing as a parameter.
main.foo(static_cast<int>(L::D));
Then your member function would be
void foo(int n)
{
std::vector<int> r(n, 9); // used `std::vector` instead of VLA
std::cout << "\n" << n << "\n";
}
(See sample code)
Note that the VLAs are not part of standard C++. Read more in the following post:
Why aren't variable-length arrays part of the C++ standard?
Prefer using std::vector as shown in the above code sample.
In a nutshell, the problem is that you have an enum that you want to use in two places. To me, the most natural solution to this is to put the enum in its own header file and use it where it is required.
// my_enum.h
#pragma once
enum class L : int {
A, B, C, D
};
// TestClass.h
#pragma once
// Can forward declare L so long as we define the functions in the same cpp
// If original enum is in a namespace it needs to be forward declared in the same namespace
enum class L;
struct TestClass {
void foo(L n);
};
// TestClass.cpp
#include "TestClass.h"
#include "my_enum.h"
void TestClass::foo(L n)
{
// do stuff with n
}
// main.cpp
#include "TestClass.h"
#include "my_enum.h"
int main(){
TestClass main;
main.foo(L::D);
return 0;
}
How to solve this (in exact place, not move enum to a scope else) ?
I'm conscious that I've answered the question in a way you did not want, but I do not see why you wouldn't want to put the enum in its own file. Avoiding this will lead to problems at some point. The consequence of JeJo's solution is that you could pass any old integer in to foo() - it is essentially decoupled from the enum. If the integer value is supposed to originate from the enum L: 1) it isn't obvious from the function signature and 2) it is prone to misuse i.e. someone passing in a value they shouldn't.
If putting the enum in its own header file is an unacceptable solution, I'd be interested to know why.
I have a doubt regarding structs and if statements in C++
For the sake of simplicity, I have created a sample code to explain my intention
int var = 10
struct example{
int a;
int b;
if(var > 8){
int c;
}
};
I have a codebase which uses similar kind of code as above. Commenting out the if portion does not give any errors.
My question is
Could if statements be put in struct declarations?
If not, what is the possible remedy for this, since if statment is mandatory.
Note: I cannot use #if,#else directives nor std::optional or other standard libraries to mitigate this, so please help me find another solution.
No you can't use if statement inside your struct or class definition. Instead, for condition declaration, you can use #if directive.
#define var 10
struct example {
int a;
int b;
#if var > 8
int c;
#endif
}
This will work.
'#if' is a compiler directive (pre-processor directive). if statement on the other hand is runtime statement.
Other than this there is no other way!
int main()
{
int var = 10;
if(var>10){
struct example{
int a;
int b;
int c;
};
}else {
struct example{
int a;
int b;
};
}
cout<<"Hello World";
return 0;
}
There are multiple ways to address your problem one way of achieving is shown.
We cannot add if Statement in the Structure.
I have some C code that has some structs that looks like this:
typedef struct my_library_a_t my_library_a_t;
typedef struct my_library_b_t my_library_b_t;
typedef struct my_library_c_t my_library_c_t;
struct my_library_a_t {
struct my_library_b_t {
int data;
struct my_library_c_t {
int data;
} c;
} b;
int data;
};
This doesn't work in C++, because in C struct my_library_b_t defines a global struct my_library_b_t, whereas in C++ it defines ::my_library_a_t::my_library_b_t.
How can I get the inner struct definition to define a global struct in C++? Or even just not have to change too much code for it to work (I don't mind having a #ifdef __cplusplus block, but I don't want to pull the structs out because the reason they are nested in the first place is that they are used only one time each, and it's really hard to read when the inner class definitions are above the outer class definitions)
I tried struct ::my_library_b_t in the definition, but this doesn't work.
For context, I'm parsing a string that has a definition that would look like this:
a = b "," data
b = data "," c
c = data
data = %x31-39 *DIGIT ; 1-9 followed by *(0-9)
/ "0"
And the intermediate parts have meaning, so it's useful to be able to have functions that take my_library_b_t* or my_library_c_t*.
I would prefer to have a solution that looks like this:
#ifdef __cplusplus
#define GLOBAL_STRUCT(name) ??? (I tried `:: name`)
extern "C" {
#else
#define GLOBAL_STRUCT(name) name
#endif
struct my_library_a_t {
struct GLOBAL_STRUCT(my_library_b_t) {
// ...
I recommend this
struct my_library_a_t {
struct my_library_b_t {
int data;
struct my_library_c_t {
int data;
} c;
} b;
int data;
};
#ifdef __cplusplus
typedef struct my_library_a_t::my_library_b_t my_library_b_t;
typedef struct my_library_b_t::my_library_c_t my_library_c_t;
#else
typedef struct my_library_a_t my_library_a_t;
typedef struct my_library_b_t my_library_b_t;
typedef struct my_library_c_t my_library_c_t;
#endif
Notice that after alias my_library_b_t, you don't need to use my_library_a_t::my_library_b_t::my_library_c_t
According to the CERT standards Object-like macros can be dangerous because their uses are not subject to the language scope rules. Can any one please explain with an example How it is problematic. say for example I have an Object-like macro #define BUFFER_SIZE 1024
// file a.hpp
#define SOME_MACRO 42
// file some/nested/dir/really/far/in/x.hpp
#define SOME_MACRO 13
Kaboom. This would have easily been avoided using any kind of scope, e.g.
struct X { static unsigned const value = 42; };
struct Y { static unsigned const value = 13; };
Now you can access X::value and Y::value. Same goes for namespaces:
namespace a { struct X; }
namespace b { struct X; }
C/C++ preprocessing is just manipulating/generating text, no language rules involved.
A classic example is
#define max 1234
...
class A
{
int arr[100];
public:
...
int max() { ... find highest in arr ... }
};
This won't compile.