1

From cppref

Like a reference, a structured binding is an alias to an existing object. Unlike a reference, the type of a structured binding does not have to be a reference type.

For example:

int a[2] = { 1, 2 };
auto [x, y] = a;

x and y are aliases rather than references. My question:

How to implement a type check function like is_alias_v<decltype(x)>?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

2 Answers2

3

I do not believe that such a thing is possible.

Fortunately, there is never any need for it.

Use x as if it were a nice juicy int, regardless of its origins. Because, well, that's what it is!

Also don't forget that x and y here don't alias or reference the elements of a, but an "invisible" copy.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

An alias is either a type alias (e.g. using Id = int) or an alias template.

What is meant by a

structured binding is an alias to an existing object

is that [x, y] as a whole is an alias (a new name) to an array of two ints (in this example). It has nothing to do with the name of the type of x alone.

If we have some type alias using Id = int, a type trait to know whether an Id is an int would be std::is_same_t<Id, int>. I don’t know how to implement a generic is_alias_t<Id>.

Nestor
  • 687
  • 5
  • 12
  • This is an interesting interpretation. But I don't think it's what cppreference wants to say. – cpplearner Aug 11 '19 at 13:45
  • What do you think cpp reference wants to say? – Nestor Aug 11 '19 at 13:49
  • 1
    A type cannot alias another type. Two types are either the same or different. A *name* can be an alias (i.e. two different names can refer to the same thing). A structured binding is another name for an existing object. Alias is an ordinary English word and cppreference doesn't have to use it only in the way the standard is using it. – n. m. could be an AI Aug 11 '19 at 14:06
  • _"A type cannot alias another type."_ Except that's precisely what aliasing means – Lightness Races in Orbit Aug 12 '19 at 00:39
  • @LightnessRacesinOrbit No it doesn't, have you read my comment to the end? – n. m. could be an AI Aug 12 '19 at 08:53
  • Yes I read your whole comment. Fortunately your comment doesn't define the English language. If a type _aliases_ another type, that literally means they are actually the same type known by different names. It's literally what "to alias" means. That's the meaning of the word. – Lightness Races in Orbit Aug 12 '19 at 13:08
  • @LightnessRacesinOrbit Alias, by definition, is an allernative *name* for the same *thing*. Look it up in the dictionary. This is exactly what I said in my comment. The crucial thing is that aliases are *different* but they name the *same thing*. It makes sense to say that a persom has several aliases (i.e. names), but not that a person aliases another person. This is nonsense. Same thing with types. – n. m. could be an AI Aug 12 '19 at 17:29
  • Guess we'll have to agree to disagree. – Lightness Races in Orbit Aug 12 '19 at 17:57