Passing anonymous struct in C++

Hi all,

I'm looking for a little advice with regard to the differences in "struct" behaviour between both C and C++ implementations.

As illustrated within the following code, I am attempting to pass an anonymous "const struct" as the parameter of a function invocation:

1
2
3
4
5
6
7
8
9
void Func1(void *arg1)
{
} 

int main(int argc, char *argv[])
{
 Func1((void *)&((const struct {int f1; char f2[sizeof("LUGA")];}) {sizeof("LUGA") - 1,"LUGA"}));
 return 0;
}


As both definition and invocation of the compound parameter are confined within the function statement itself, this of course facilitates the eventual construction of a "length, string" pre-processor compilation macro.

When compiled within a standard C environment this is indeed the case, with both length/string components situated in adjacent memory/file locations and the appropriate const-segment address passed during run-time.

However, due to the inherent "class" nature of "struct" within an OOP environment, this unfortunately is not the case when compiling the same code under C++. In contrast to the contiguous generation of compound-PODs within the executable's const-segment, the "class" style "struct" is temporarily constructed during function invocation prior to stacking its "volatile" address during parameter exchange. Needless to say, the majority of C++ compilers diligently warn against the possible dangers of referencing temporary object addresses.

With this in mind, my question is but a simple one: does anyone know a way in which I can emulate the aforementioned C behaviour within a C++ environment?

All comments gratefully received.
It compiles under GNU C++ and fails under Microsoft Visual Studio 2005.

I dunno what the standard thinks.
Why are you trying to do that, may I ask?
This is indeed a good question; coming from an ASM/C background I am by no means a C++ expert and there may indeed be an easier way to achieve my underlying objective.

Having developed my own string class as part of the obligatory C++ initiation process, I am looking for an alternative yet convenient method of supporting generic C style string literals.

For example, assuming a bog-standard reference-counted class implementation, the following code structure would certainly be representative of "expected" usage:

1
2
3
BogStandardClass mystring1("Hello");
....
BogStandardClass mystring2(mystring1 + " and goodbye!");


However, in the vain hope of avoiding both buffer copy and strlen overhead, I was attempting to generate a preprocessor macro which would convert the statement _mymacro("Hello") into the appropriate length/string pair during compilation. Providing the pair is generated as a contiguous compound-POD as per the C implementation, I am able to directly reference the pre-compiled literal from within the executable' s const-segment and thus avoid unnecessary strlen and copy operations during object initialisation.

Thus, assuming the presence of a functional macro, the above code sequence could be rewritten as follows:

1
2
3
BogStandardClass mystring1(_mymacro("Hello"));
....
BogStandardClass mystring2(mystring1 + _mymacro(" and goodbye!"));


When all is said and done, what I'm effectively trying to do is coerce the way in which C/C++ compilers generate string literals within the executable's const-segment; from ASCIIZ to a custom length-prefixed format. However, considering the various strlen compile-time optimisations in conjunction with "standard" string usage, this entire question is more likely to procure academic interest than any significant performance optimisations.
Would you be satisfied with
BogStandardClass mystring2(mystring1) + " and goodbye!";

The standard library string class supports mixing C strings.

In general C++ has a much less need for the preprocessor. It's a technique you may want to put aside. There a chapter on C++'s aims regarding the C Preprocessor in The Design and Evolution of C++.
Topic archived. No new replies allowed.