3

I am reposting a comp.std.c++ Usenet discussion here because that group has become very unreliable. The last few posts I've submitted there have gone into the void, and activity has all but ceased. I doubt I've been banned and/or everyone else just lost interest. Hopefully all interested people will find this discussion, and there will be a general migration. Maybe then they will appoint a new moderator.


Hello!

With my current interpretation of draft N3126 w.r.t. the conditional operator and xvalues, I expect the following assertions to hold:

 int i = 0;
 int& j = true? i : i;
 int&& k = true? std::move(i) : std::move(i);   // #2
 assert(&i == &j); // Holds since C++98
 assert(&i == &k); // Should this hold as well?

5.16/4 says:

If the second and third operands [to the conditional operator] are glvalues of the same value category and have the same type, the result is of that type and value category [...]

Though, it doesn't clearly say that the resulting glvalue refers to one of the objects the glvalue operands referred to -- or is this implied because otherwise it would return a prvalue? Using GCC 4.5.1 in C++0x mode the second assertion fails. The reference k seems to refer to some temporary object. Can somebody clarify whether the comiler is allowed to create such a temporary in case both operands around the colon are xvalues of the same type?

I'm currently assuming GCC is buggy and/or not up-to-date with respect to xvalues.

The followup question is: Wouldn't it be nice to be able to detect the value category of an expression? If we ignore the conditional operator we can detect the value category of an expression with decltype. But what is

 bool xvalue = std::is_rvalue_reference<
   decltype( true ? std::move(i) : std::move(i) ) >::value;

supposed to yield? Using GCC 4.5.1, the xvalue variable is initialized with false. Is this conforming to the current standard draft?

TIA, Sebastian

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • I think you mean [comp.std.c++], as implied by your link. Yes it does have some lag time but I think you may have just encountered a recent Google blackout. Did you post via Google Groups? – Cheers and hth. - Alf Oct 11 '10 at 20:41
  • @Alf: I do post by Google Groups, but wouldn't that affect all groups? There is virtually no activity at comp.std.c++ since September, not so for the unmoderated groups. – Potatoswatter Oct 11 '10 at 20:48
  • From here c.l.c++ has 30 postings for my local "today", c.s.c++ has two (one of them from "SG", which seems to be you), and another five messages for earlier from other people this month. – sbi Oct 11 '10 at 20:49
  • @sbi: I'm David Krauss on comp.lang.c++ (and real life); this post is community wiki because I'm reposting on someone else's behalf. `comp.lang.c++` is unmoderated, so it has more activity because there is no moderator asleep at the wheel. `comp.std.c++` has seven messages for October vs. dozens for September because the moderator has all but quit. I don't know why or how he manages to let such a small, yet nonzero, trickle through. – Potatoswatter Oct 11 '10 at 20:58
  • Since `comp.std.c++` is a semi-official channel for submitting defect reports, I've created the `c++-defect` tag to correspond to that sort of thing. Maybe there can be a process for forwarding questions to the committee once they've been vetted. – Potatoswatter Oct 11 '10 at 21:05
  • 1
    There are supposedly [five moderators](http://www.comeaucomputing.com/csc/faq.html). Have you tried contacting them (std-c++-request@cs.rpi.edu)? – James McNellis Oct 11 '10 at 21:08
  • @James: No, I'm not the boss of them and I'm not an activist, at least not within the ISO committee framework (until if and when they recognize me, I suppose). If they don't want to do their job, and if the community can get by without them, they're free not to do it. But it's plainly apparent they're not. – Potatoswatter Oct 11 '10 at 21:15
  • Fair enough. For what it's worth, there are instructions on how to submit a library defect on the [LWG defects page](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html) basically saying just to send an e-mail Alisdair Meredith, LWG chairman (Howard Hinnant, who was LWG chairman until September, said he hadn't gotten a notification from the newsgroup moderators in a few years). For language defects, the CWG defects page just refers to the comp.std.c++ FAQ for how to submit a defect; I suppose it couldn't hurt to e-mail Mike Miller to see what the best way to get defects to them is. – James McNellis Oct 11 '10 at 21:25
  • @James: Yep. For that matter, I've sent 3 emails to Alisdair in the past month with no reply. Probably because they consist of a minor spelling correction, a dangling reference correction, and a request to specialize `std::tuple_element` and `tuple_size` on `bitset`. So I don't worry too much. But submitting a detailed two-page answer to `comp.std.c++` seems a lot like just feeding someone's spam filter. – Potatoswatter Oct 11 '10 at 21:34
  • 2
    You can also e-mail Pete Becker (editorial@versatilecoding.com) with typographical and editorial errors (like a spelling error). The whole defect reporting process is too confusing, IMO. It'd be nice if the committee had Bugzilla or Trac or something that made it easy to report issues (of course, they'd probably say the correct way to submit defects is to join the committee, but the $1,200/year fee is a bit steep for that privilege). – James McNellis Oct 11 '10 at 21:46
  • @James McNellis: You can't join the committee directly; ISO's members are the national organizations (in your case, INCITS(USA)) and national organisatsions usually have company memberships. Your profile suggests you work at Microsoft, which has already paid the INCITS fee and is active within the ISO committee. – MSalters Oct 12 '10 at 08:28
  • 2
    @MSalters: I meant that half-jokingly; every time I've interacted with committee members they have been very helpful. Anyway, in general, if someone wanted to formally participate, they'd have to join; for those in the US, the fee for joining INCITS J16 is $1,200/year. I know that if I wanted to and had the time to participate, I could probably do so through my employer, but many people don't have that option. Of course, having a high barrier to entry probably keeps the noise level down, which is usually a good thing. – James McNellis Oct 12 '10 at 16:10

1 Answers1

2

I think GCC 4.5.1 is nonconforming wrt §5.16/4. Have you filed a bug report?

Anyway, I think it is conforming with that ternary operator code. decltype is defined by §7.1.6.2/4:

The type denoted by decltype(e) is defined as follows:

  • if e is an unparenthesized id-expression or a class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;
  • otherwise, if e is a function call (5.2.2) or an invocation of an overloaded operator (parentheses around e are ignored), decltype(e) is the return type of the statically chosen function;
  • otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
  • otherwise, decltype(e) is the type of e. The operand of the decltype specifier is an unevaluated operand (Clause 5).

decltype works by fetching the appropriate declaration and returning the desired type from it. It has little intelligence with respect to non-overloaded operators. Perhaps another point

  • otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e

would be in order, particularly since, as written, xvalues get treated as prvalues. Furthermore, your expression corresponds exactly to the definition of std::common_type (§20.7.6.6/3).

One straightforward workaround (to coin a phrase :vP ):

template< typename T1, typename T2 >
struct common_type_and_category {
    typedef typename std::conditional<
        std::is_same< T1, T2 >::value,
        T1,
        typename std::common_type< T1, T2 >::type
    >::type type;
};
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421