2

I have class that represents an array reference (class array_ref) and another that is (i.e. holds/own/contains) the array (class array). array_ref behaves like a reference.

Does it make sense to specialize std::decay for class array_ref to be array?

namespace std{
template<> class decay<arra_ref>{typedef array type;};
}

What other alternatives do I have to tell generic programs that array is the "value type" of array_ref?

Is std::decay used in any STL algorithm?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
alfC
  • 14,261
  • 4
  • 67
  • 118
  • 1
    No. Decay is a very specific quirk of C arrays and functions. `auto foo = obj` is expected to decay arrays and functions to pointers and function pointers. You can't reproduce that with your `array_ref` type. – zneak Dec 08 '16 at 01:30

1 Answers1

3

It doesn't matter whether standard library algorithms use it or not. What matters is what the standard says in [meta.type.synop]/1:

The behavior of a program that adds specializations for any of the templates defined in this subclause is undefined unless otherwise specified.

Included in "this subclause" are all of the type-traits classes, including decay. So don't specialize it. Ever.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    Ok, thanks. I think there should be a *convention* to get the "value type" of other types that are references and "not value types" themselves. (see the comments in http://stackoverflow.com/a/40892183/225186) – alfC Dec 08 '16 at 01:55
  • Maybe, we need a library (or std) trait for references `[std::]reference_traits::value_type` (which nice defaults like `reference_traits::value_type == T`). – alfC Sep 27 '22 at 18:54