6
template<typename T>
concept Octet = 1 == sizeof(T);

// ok
Octet decltype(auto) c = 'a';

// ok
void f1(const auto&) {}

// ok
void f2(Octet auto) {}

// ok
void f3(Octet auto&&) {}

// error: expected ‘auto’ or ‘decltype(auto)’ after ‘Octet’
void f4(Octet const auto&) {}

// error: cannot declare a parameter with ‘decltype(auto)’
void f5(Octet decltype(auto)) {}

Compiled with gcc-11 -std=c++20. See: https://godbolt.org/z/xK769Pfjn

Why do f4 and f5 not work?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • 3
    Hmmm is the language finally enforcing right const? – JHBonarius May 07 '21 at 18:20
  • 2
    `Octet auto` represents the type. You can have right const or left const, but not "middle-const". – Drew Dormann May 07 '21 at 18:22
  • Why does `f5` not work? @DrewDormann – xmllmx May 07 '21 at 18:24
  • please add the complete compiler error message. I suppose this is gcc, because clangs error is different (although not much more helping) – 463035818_is_not_an_ai May 07 '21 at 18:24
  • 1
    All necessary messages are included. @largest_prime_is_463035818 – xmllmx May 07 '21 at 18:28
  • 1
    @xmllmx `f5` is not valid C++ syntax. It has no meaning. You could perhaps look at [this question](https://stackoverflow.com/questions/24109737) to see the contexts where `decltype(auto)` has meaning in C++. Or perhaps you could [edit] the question to describe what you think that parameter would mean. – Drew Dormann May 07 '21 at 18:32

1 Answers1

5

As seen in [dcl.spec.auto], when you use a placeholder here, the constraint needs to immediately precede the auto:

placeholder-type-specifier:
type-constraint_opt auto
type-constraint_opt decltype ( auto )

This is simply a matter of syntax. The constraint isn't a general specifier like const is; it doesn't have a flexible position. Conceptually, you can think of Octet auto as one "word" representing the constrained type.

As for f5, this is not allowed as a parameter per p2:

A placeholder-type-specifier of the form "type-constraint_opt auto" can be used as a decl-specifier of the decl-specifier-seq of a parameter-declaration of a function declaration...

No such text exists for decltype(auto). In addition, per p6:

A program that uses a placeholder type in a context not explicitly allowed in this subclause is ill-formed.

Logically, I'm not sure how I'd specify decltype(auto) to work in this context. Sure, it could be specified, but I don't think there's precedent in the language for it, so it would need to be motivated over an alternative that already has the desired effect.

chris
  • 60,560
  • 13
  • 143
  • 205