Indiana Unversity logo[ConceptGCC]

ConceptGCC :

Shouldn't Assignable concepts stop supporting assignment-to-rvalue?

From: Niels Dekker (nielsprogramming_at_[hidden])
Date: 2009-01-06 13:59:50


When a user accidentally assigns a value to a temporary of an assignable
type, ConceptGCC may or may not reject the assignment, depending on the
specific type. ConceptGCC doesn't detect such an error until the
template that has the assignment is instantiated. For example:

  ////////////////////////////////////////////////////////////
  #include <concepts>

  template<typename T>
  requires std::DefaultConstructible<T> && std::CopyAssignable<T>
  void assign_to_rvalue(const T& arg)
  {
    T() = arg;
  }

  struct foo { int i; };

  int main()
  {
    const foo obj = { 42 };
    assign_to_rvalue(obj); // accepted by ConceptGCC.

    const int i = 42;
    assign_to_rvalue(i); // rejected.
  }

  ////////////////////////////////////////////////////////////

ConceptGCC 4.3.0 alpha 7 accepts the template itself, and only rejects
the instantiation with T = int:

  In function 'void assign_to_rvalue(const T&) [with T = int]':
  19: instantiated from here
  8: error: lvalue required as left operand of assignment

Wouldn't it be preferable to deal with assignment within a
concept-checked template in a more generic way? I think I would prefer
disallowing assignment to a temporary, if its type is only specified to
be CopyAssignable. (FWIW, I wouldn't mind if the user could possibly
add an "AssignableToRvalue" concept, if she really needs to assign to a
temporary. But I guess such a use case is rather exotic.)

Note: this question of mine isn't specifically meant to be a ConceptGCC
feature request. I'm just interested to hear your opinion. Especially
because I'm working on a resolution of a library issue, proposing to
disallow assignment to an rvalue for most of the Standard Library types:
"Ref-qualifiers for assignment operators",
http://home.roadrunner.com/~hinnant/issue_review/lwg-active.html#941

Kind regards, Niels

--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center