What's the closest thing to an anonymous struct/array I can achieve that compiles with GCC?
-
I am writing code for an ARM processor and compiling with GCC. I have done some C# programming and there you're allowed to pass anonymous arrays and structs directly in method calls:
MyClass.MyMethod1(new byte[]{0, 1, 2, 3}); // This is allowed in C#
and
MyClass.MyMethod2(new MyStruct("", 0, null)); // This is allowed in C#
What's the closest thing I can achieve the same thing in my ARM-project?
myFunction1({0, 1, 2, 3}); // What I want (or something similar) that compiles in GCC
myFunction2({"", 0, NULL}); // What I want (or something similar) that compiles in GCCI understand that I could change my function to take a function pointer instead of a struct as a parameter using the technique described in Re: What's the closest thing to anonymous function pointers I can achieve that compiles with GCC? - C / C++ / MFC Discussion Boards[^]:
#include
using namespace std;
using getMyStructCallback_t = function ;void myFunction(getMyStructCallback_t getMyStructCallback); // Declaration as it appears in the h-file
myFunction([]() {
static struct MyStruct myStruct = {"", 0, NULL}; // What I'm aware is possible, but would like to avoid
return &myStruct;} // What I'm aware is possible, but would like to avoid
);but if possible, then I would like to pass my struct/array directly.
-
I am writing code for an ARM processor and compiling with GCC. I have done some C# programming and there you're allowed to pass anonymous arrays and structs directly in method calls:
MyClass.MyMethod1(new byte[]{0, 1, 2, 3}); // This is allowed in C#
and
MyClass.MyMethod2(new MyStruct("", 0, null)); // This is allowed in C#
What's the closest thing I can achieve the same thing in my ARM-project?
myFunction1({0, 1, 2, 3}); // What I want (or something similar) that compiles in GCC
myFunction2({"", 0, NULL}); // What I want (or something similar) that compiles in GCCI understand that I could change my function to take a function pointer instead of a struct as a parameter using the technique described in Re: What's the closest thing to anonymous function pointers I can achieve that compiles with GCC? - C / C++ / MFC Discussion Boards[^]:
#include
using namespace std;
using getMyStructCallback_t = function ;void myFunction(getMyStructCallback_t getMyStructCallback); // Declaration as it appears in the h-file
myFunction([]() {
static struct MyStruct myStruct = {"", 0, NULL}; // What I'm aware is possible, but would like to avoid
return &myStruct;} // What I'm aware is possible, but would like to avoid
);but if possible, then I would like to pass my struct/array directly.
Check this:
struct MyStruct
{
const char* s;
int i;
void* ptr;
};class MyClass
{
public:
MyClass ();
void MyMehod1 (const std::vector& v) {};
void MyMethod2 (const MyStruct& s) {};
};int main()
{MyClass c;
c.MyMehod1 ({ 0, 1, 2, 3 });
c.MyMethod2 ({ "", 0, nullptr });
}Mircea
-
Check this:
struct MyStruct
{
const char* s;
int i;
void* ptr;
};class MyClass
{
public:
MyClass ();
void MyMehod1 (const std::vector& v) {};
void MyMethod2 (const MyStruct& s) {};
};int main()
{MyClass c;
c.MyMehod1 ({ 0, 1, 2, 3 });
c.MyMethod2 ({ "", 0, nullptr });
}Mircea
-
Your question had this code:
MyClass.MyMethod1(new byte[]{0, 1, 2, 3});
Here you a calling a method (also called member function) of an object. So your code is already object oriented. Maybe you should revise/clarify what you want to accomplish.
Mircea
-
Your question had this code:
MyClass.MyMethod1(new byte[]{0, 1, 2, 3});
Here you a calling a method (also called member function) of an object. So your code is already object oriented. Maybe you should revise/clarify what you want to accomplish.
Mircea
That was a C# example, to indicate that I was looking for the corresponding C/C++ syntax for my ARM-project. I try to keep my ARM-code as close to pure C-programming as possible (this will obviously be an exception) and never use classes in the ARM-project. Is anything in your example dependent on the classes or will it compile just fine without classes? I'm not at work so I can't try it for myself right now, but I plan to work on this first thing in the morning.
-
I am writing code for an ARM processor and compiling with GCC. I have done some C# programming and there you're allowed to pass anonymous arrays and structs directly in method calls:
MyClass.MyMethod1(new byte[]{0, 1, 2, 3}); // This is allowed in C#
and
MyClass.MyMethod2(new MyStruct("", 0, null)); // This is allowed in C#
What's the closest thing I can achieve the same thing in my ARM-project?
myFunction1({0, 1, 2, 3}); // What I want (or something similar) that compiles in GCC
myFunction2({"", 0, NULL}); // What I want (or something similar) that compiles in GCCI understand that I could change my function to take a function pointer instead of a struct as a parameter using the technique described in Re: What's the closest thing to anonymous function pointers I can achieve that compiles with GCC? - C / C++ / MFC Discussion Boards[^]:
#include
using namespace std;
using getMyStructCallback_t = function ;void myFunction(getMyStructCallback_t getMyStructCallback); // Declaration as it appears in the h-file
myFunction([]() {
static struct MyStruct myStruct = {"", 0, NULL}; // What I'm aware is possible, but would like to avoid
return &myStruct;} // What I'm aware is possible, but would like to avoid
);but if possible, then I would like to pass my struct/array directly.
You could do something like:
void Function(const std::vector& values)
Call it as follows:
Function(std::vector{ 5, 4, 3});
(Works with VC++ 2017. Should work with GCC.)
-
That was a C# example, to indicate that I was looking for the corresponding C/C++ syntax for my ARM-project. I try to keep my ARM-code as close to pure C-programming as possible (this will obviously be an exception) and never use classes in the ARM-project. Is anything in your example dependent on the classes or will it compile just fine without classes? I'm not at work so I can't try it for myself right now, but I plan to work on this first thing in the morning.
arnold_w wrote:
I try to keep my ARM-code as close to pure C
arnold_w wrote:
I plan to work on this first thing in the morning.
When you get back into the office please print out
__STDC_VERSION__
so we know which language version you are working under. The C99 language and below had no support for anonymous structures or unions. But the GCC compiler has a non-standard extension -std=gnu99 that enables it. Support for anonymous structures or unions was added in C11[^] back in 2011 If your__STDC_VERSION__
is 199901L or above then you should be able to use anonymous objects. Best Wishes, -David Delaune -
You could do something like:
void Function(const std::vector& values)
Call it as follows:
Function(std::vector{ 5, 4, 3});
(Works with VC++ 2017. Should work with GCC.)
Joe Woodbury wrote:
void Function(const std::vector<uint8_t>& values)
Is there anything preventing me from having an override pure C-function (in addition to your C++ function) as follows:
void Function(uint8_t* values)
? I'm thinking that in that case I would have functions that can be called both from files with .c-extension and .cpp-extension? I'd image const std::vector stores bytes in memory exactly the same way uint8_t* (or uint8_t[]) does, right?
-
Joe Woodbury wrote:
void Function(const std::vector<uint8_t>& values)
Is there anything preventing me from having an override pure C-function (in addition to your C++ function) as follows:
void Function(uint8_t* values)
? I'm thinking that in that case I would have functions that can be called both from files with .c-extension and .cpp-extension? I'd image const std::vector stores bytes in memory exactly the same way uint8_t* (or uint8_t[]) does, right?
In C++, you could overload Function, but you'd have to do the normal #ifdef stuff to call Function from C. (Yes, by standard std::vector stores data contiguously.) The problem is that the C function is rather dangerous.
-
I am writing code for an ARM processor and compiling with GCC. I have done some C# programming and there you're allowed to pass anonymous arrays and structs directly in method calls:
MyClass.MyMethod1(new byte[]{0, 1, 2, 3}); // This is allowed in C#
and
MyClass.MyMethod2(new MyStruct("", 0, null)); // This is allowed in C#
What's the closest thing I can achieve the same thing in my ARM-project?
myFunction1({0, 1, 2, 3}); // What I want (or something similar) that compiles in GCC
myFunction2({"", 0, NULL}); // What I want (or something similar) that compiles in GCCI understand that I could change my function to take a function pointer instead of a struct as a parameter using the technique described in Re: What's the closest thing to anonymous function pointers I can achieve that compiles with GCC? - C / C++ / MFC Discussion Boards[^]:
#include
using namespace std;
using getMyStructCallback_t = function ;void myFunction(getMyStructCallback_t getMyStructCallback); // Declaration as it appears in the h-file
myFunction([]() {
static struct MyStruct myStruct = {"", 0, NULL}; // What I'm aware is possible, but would like to avoid
return &myStruct;} // What I'm aware is possible, but would like to avoid
);but if possible, then I would like to pass my struct/array directly.
Today I saw the following syntax:
uint16_t getUint16Value(int someParameter) {
// Unimportant implementation
}void myFunction() {
uint16_t destArray[2];
memcpy(destArray, (uint16_t[2]){getUint16Value(0), getUint16Value(1)}, sizeof(uint16_t[2]));
}Now, what made me really surprized about this code is that it was inside a .c-file, not a .cpp-file! I looked in the GCC documentation and tried to find examples of clever syntax like this, but I couldn't find any. Does anybody know where I can examples of anonymuous "things" (arrays, structs, function-pointers, etc) that GCC allows in C-code?
-
Joe Woodbury wrote:
void Function(const std::vector<uint8_t>& values)
Is there anything preventing me from having an override pure C-function (in addition to your C++ function) as follows:
void Function(uint8_t* values)
? I'm thinking that in that case I would have functions that can be called both from files with .c-extension and .cpp-extension? I'd image const std::vector stores bytes in memory exactly the same way uint8_t* (or uint8_t[]) does, right?
Note that C arrays do not include length information! So unless your function
Function(uint8_t*)
somehow knows how many elements there are, you'd also have to pass the number of elements as a separate parameter! And even if it does know how many elements to expect, there's always a chance someone calls it accidentally with a different number of elements. The class std::vector behaves like an array, but internally it does store the array length which makes the code examples above feasible. You could of course write a C++ wrapper that calls a C function internally, like this:void Function(uint8_t* values, unsigned long int size); //normal C function implemented elsewhere
void Function(const std::vector& values) {
Function(values.data(), values.size());
}
int main() {
Function({1, 2, 3});
return 0;
}Otherwise, you'd have to think of another way to pass the element number reliably.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)