Re: [PATCH] gettext: more-consistent no-op macros

Bruno Haible via Gnulib discussion list <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Message-ID <5426519.RGdJG60b3L@cagnes>
I wrote:
> clang in C++ mode accepts the compound initializer syntax
>   (TYPE) { VALUE... }
> but — unlike gcc in C++ mode — requires that the values be constant
> expressions.

But that is not universally true. It depends on the context.

In any case, static_cast<TYPE> works fine regardless of C++ version
and C++ compiler.

Test program below.

Bruno

================================ foo.cc ================================
/* Compile this with g++ or clang++ >= 4.  */

extern int foo;
extern int bar ();
struct s { int x; };

/* In normal expression context: */

int f1 (void) {
  return (int){ foo } + (int){ bar () };
}
#if __cplusplus >= 201100
int f2 (void) {
  return int{ foo } + int{ bar () };
}
#endif
struct s f3 (void) {
  return (struct s){ foo };
};
struct s f4 (void) {
  return (struct s){ bar () };
};
struct s f5 (void) {
  return (struct s){ .x = foo };
};
struct s f6 (void) {
  return (struct s){ .x = bar () };
};
#if __cplusplus >= 201100
struct s f7 (void) {
  return s{ foo };
};
struct s f8 (void) {
  return s{ bar () };
};
#endif
int f9 (void) {
  return static_cast<int> (foo) + static_cast<int> (bar ());
}

/* In initializer context: */

#if !defined __clang__ /* error: initializer element is not a compile-time constant */
int v1 = (int){ foo } + (int){ bar () };
#if __cplusplus >= 201100
int v2 = int{ foo } + int{ bar () };
#endif
struct s v3 = (struct s){ foo };
struct s v4 = (struct s){ bar () };
struct s v5 = (struct s){ .x = foo };
struct s v6 = (struct s){ .x = bar () };
#endif
#if __cplusplus >= 201100
struct s v7 = s{ foo };
struct s v8 = s{ bar () };
#endif
int v9 = static_cast<int> (foo) + static_cast<int> (bar ());
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.