Sunday, July 10, 2016

Digest for comp.lang.c++@googlegroups.com - 9 updates in 1 topic

"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Jul 09 08:31PM -0700

I have something like this and I need to assign a value to the second member
of the union:
 
struct SAbc
{
int discriminator;
 
union {
int value;
char* ptr;
};
};
 
When I use it in source code, it's an example like this:
 
SAbc abc[] =
{
{ 1, 1 }, // Initializes discriminator, value
{ 2, "test" }, // Should initialize discriminator, ptr
};
 
I remember from GCC a syntax which was something like this:
 
{ 2, {.ptr = "test"} },
 
But I can't figure out how to get it to work in Microsoft's C++ compiler,
and I don't know what to search for to find it via Google.
 
Best regards,
Rick C. Hodgin
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jul 10 05:06AM +0100

On 10/07/2016 04:31, Rick C. Hodgin wrote:
 
> { 2, {.ptr = "test"} },
 
> But I can't figure out how to get it to work in Microsoft's C++ compiler,
> and I don't know what to search for to find it via Google.
 
Use boost.variant and then when it becomes available use std::variant.
 
/Flibble
"Öö Tiib" <ootiib@hot.ee>: Jul 10 04:39AM -0700

On Sunday, 10 July 2016 06:31:56 UTC+3, Rick C. Hodgin wrote:
 
> { 2, {.ptr = "test"} },
 
> But I can't figure out how to get it to work in Microsoft's C++ compiler,
> and I don't know what to search for to find it via Google.
 
I am making three assumptions (assuming "yes" to three questions below).
1) PODs and aggregates are used to ensure that minimal run-time calls
are made?
2) 'char*' initialized from pointer to string literal won't be ever
used as pointer to mutable char?
3) discriminator is always 1 on case of int and always 2 on case of
string literal?
 
If I'm wrong, use 'boost::variant' instead of 'union' ... otherwise here
is my attempt:
 
// the class
struct SAbc
{
int discriminator;

union
{
int value;
char const* ptr;
};
 
constexpr explicit SAbc(int v)
: discriminator(1)
, value(v)
{}
 
constexpr explicit SAbc(char const* p)
: discriminator(2)
, ptr(p)
{}
};
 
// the array
constexpr SAbc abc[] =
{
SAbc(1),
SAbc("test"),
};
Frank Tetzel <s1445051@mail.zih.tu-dresden.de>: Jul 10 03:29PM +0200


> { 2, {.ptr = "test"} },
 
> But I can't figure out how to get it to work in Microsoft's C++
> compiler, and I don't know what to search for to find it via Google.
 
This is called designated initializers[1] and is part of the C99
standard, not in any C++ standard as far as i know. GCC allows it in
C++ code nonetheless as a GNU extension, similar to
variable-length-arrays. Clang supports it too but as Visual Studio only
has a recent C++ compiler it mostly likely does not.
 
Just define two overloaded ctors for the structure and use normal list
initialization.
 
struct SAbc
{
int discriminator;
 
union {
int value;
const char *ptr;
};
 
SAbc(int discriminator, int value)
: discriminator(discriminator), value(value) {}
SAbc(int discriminator, const char *ptr)
: discriminator(discriminator), ptr(ptr) {}
};
 
 
[1] https://gcc.gnu.org/onlinedocs/gcc-6.1.0/gcc/Designated-Inits.html
 
Regards,
Frank
woodbrian77@gmail.com: Jul 10 09:54AM -0700

On Sunday, July 10, 2016 at 6:40:27 AM UTC-5, Öö Tiib wrote:
> SAbc(1),
> SAbc("test"),
> };
 
I prefer your approach to Frank's, but I'd encourage
the OP to reconsider the use of the union. He may
not need it and at the least he should improve the
name of the struct -- "SAbc" doesn't cut it.
 
Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Jul 10 12:26PM -0700

Brian Wood wrote:
> ... at the least he should improve the name of the struct
> -- "SAbc" doesn't cut it.
 
It was an example. The actual name is appropriately descriptive.
 
I appreciate everyone's response. I like the solutions offered.
I have a need in this project to use unions, so I need the syntax
to allow the content to be populated. If it's not possible in MSVC++,
I'll use GCC for this part and link it in. I have already done this
for another need which GCC allowed, but MSVC++ did not.
It allowed constant text to be assigned to a pointer, but in
readwrite memory, rather than readonly.
 
Best regards,
Rick C. Hodgin
Kalle Olavi Niemitalo <kon@iki.fi>: Jul 10 11:02PM +0300

> for another need which GCC allowed, but MSVC++ did not.
> It allowed constant text to be assigned to a pointer, but in
> readwrite memory, rather than readonly.
 
Huh? Can't you do that portably:
 
static char readwrite[] = "some text you want";
char *pointer = readwrite;
 
Then assign pointer[2] = 'l' as you like.
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Jul 10 02:52PM -0700

On Sunday, July 10, 2016 at 4:02:44 PM UTC-4, Kalle Olavi Niemitalo wrote:
 
> static char readwrite[] = "some text you want";
> char *pointer = readwrite;
 
> Then assign pointer[2] = 'l' as you like.
 
It refers to this form:
 
http://compgroups.net/comp.lang.c++/link-object-files-from-vc++-and-gcc/2080401
 
By using MinGW and compiling the data and then linking to the MSVC++
project, the constant data by pointer is in readwrite memory.
 
Best regards,
Rick C. Hodgin
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Jul 10 02:54PM -0700

On Sunday, July 10, 2016 at 4:02:44 PM UTC-4, Kalle Olavi Niemitalo wrote:
 
> static char readwrite[] = "some text you want";
> char *pointer = readwrite;
 
> Then assign pointer[2] = 'l' as you like.
 
Here are the details I created from that reference:
 
https://groups.google.com/d/msg/comp.lang.c/UXGdxAzPgrg/h9NIIMAPf4kJ
 
Best regards,
Rick C. Hodgin
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: