Yes, they should standardize it (instead of adding useless bloat) since all mainstream compilers seem to support anonymous structs anyway and they are useful.
One side note, I guess there's a typo and assignment operators should return a reference; not that it would matter because one will usually use one += per expression.
eval type
Moderator: Ras
-
mar
- Posts: 2684
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
-
Rein Halbersma
- Posts: 771
- Joined: Tue May 22, 2007 11:13 am
Re: eval type
Why would you worry about move semantics? For a simple struct wrapper around a couple of builtins, the compiler generated move constructor/assignment operator will do the right thing.lucasart wrote: 3/ portable C++
Package two int in some structure and define operators. Either of:Advantage: vectorized syntaxCode: Select all
typedef int[2] eval_t; typedef std::array<int, 2> eval_t; struct eval_t { int op, eg; }; std::pair<int, int>;
Disadvantage: you need to define tons of operators (+move semantic).
Note also that you can write a macro to generate the operators for you. Just write an operator@= in-class and the corresponding operator@ out-of-class in terms of the former. So if T is your eval_t struct with internal data v[2], and you currently have a bunch of code for @=+,-,*,/ etc.
Code: Select all
struct T
{
T& operator@=(T const& other) { v[0] @= other[0]; v[1] @= other[1]; return *this; }
};
T operator@(T const& L, T const& R) { return T(L) @= R; }
Code: Select all
struct T
{
GENERATE_OP_ASSIGN(+) // repeat for -, *, / etc.
};
GENERATE_OP(+) // repeat for -, *, / etc.
-
mar
- Posts: 2684
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: eval type
Not a bad idea using macros, I don't do that but it makes sense as it avoids potential copy-paste errors 
btw. why not
As for boost, well, you know my opinion
100MB worth of code/1.1M lines (for comparison: Unreal Engine 4 has 1.2M loc,
but to be fair boost probably contains lots of generated code for pre-C++11 compatibility)
=> a nasty dependency if you ask me, they should break it into smaller pieces but I wouldn't use it anyway - YMMV.
btw. why not
Code: Select all
T operator@(T L, T const& R) { return L @= R; }
but to be fair boost probably contains lots of generated code for pre-C++11 compatibility)
=> a nasty dependency if you ask me, they should break it into smaller pieces but I wouldn't use it anyway - YMMV.
-
Rein Halbersma
- Posts: 771
- Joined: Tue May 22, 2007 11:13 am
Re: eval type
There are many variations on this theme, IIRC the best thing to do is to use the NRVO and writemar wrote:Not a bad idea using macros, I don't do that but it makes sense as it avoids potential copy-paste errors
btw. why notCode: Select all
T operator@(T L, T const& R) { return L @= R; }
Code: Select all
T operator@(T const& L, T const& R) { T nrv(L); nrv @= R; return nrv; }
-
mar
- Posts: 2684
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: eval type
I think all modern compilers do RVO.
The only exception is perhaps Microsoft compiler which has problems with custom dtors (even if the body does nothing):
The workaround is to disable exceptions 
I'm not sure if they fixed this issue but IIRC 2012 had this problem. I bet gcc/clang won't have problems with this, but I haven't tried.
The only exception is perhaps Microsoft compiler which has problems with custom dtors (even if the body does nothing):
Code: Select all
struct s {
~s() {}
};
I'm not sure if they fixed this issue but IIRC 2012 had this problem. I bet gcc/clang won't have problems with this, but I haven't tried.
-
lucasart
- Posts: 3243
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: eval type
Are you sure about that? I've always thought that the result of += is an r-value, not an l-value, and that:mar wrote:One side note, I guess there's a typo and assignment operators should return a reference; not that it would matter because one will usually use one += per expression.
Code: Select all
a += b += c;Code: Select all
a += (b += c);Edit: You're right!
https://en.wikipedia.org/wiki/Operators ... nd_C%2B%2B
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.