1

I wrote an answer here to use qsort to sort an array-of-arrays. I could not use sort on account of it's use of the assignment operator in swap operations.

I believe that the stipulation on my answer working is:

The type of the elements of the array must be a TrivialType, otherwise the behavior is undefined

So my question is: Is int[2] a "TrivialType"?


The actual code in answer that prompted this question is:

int array[5][2] = { {20, 11}, {10, 20}, {39, 14}, {29, 15}, {22, 23} };
static const auto SIZE = size(*array);

qsort(array, size(array), sizeof(*array), [](const auto lhs, const auto rhs) {
    const auto first = reinterpret_cast<const int*>(lhs);
    const auto last = next(first, SIZE);
    const auto its = mismatch(first, last, reinterpret_cast<const int*>(rhs));

    if (its.first == last) {
        return 0;
    } else if (*its.first < *its.second) {
        return -1;
    } else {
        return 1;
    }});
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • You needed to follow the links: http://en.cppreference.com/w/cpp/types/is_trivial: *that is, a scalar type, a trivially copyable class with a trivial default constructor, or array of such type/class, possibly cv-qualified* – NathanOliver Jul 07 '16 at 17:38
  • IIRC you should prefer `std::sort()` over `qusort()`. – πάντα ῥεῖ Jul 07 '16 at 17:39
  • @πάνταῥεῖ You read the question right? I explained right in the question why that wouldn't work. – Jonathan Mee Jul 07 '16 at 17:41
  • If you replace the C arrays with `std::array` your problem disappears. – nwp Jul 07 '16 at 17:41
  • @nwp Or `vector` or any other container that is copy assignable. I mention that in my answer that prompted this question, but my intent here is to find out if this is legal for arrays. – Jonathan Mee Jul 07 '16 at 17:44
  • @NathanOliver Your quote may answer my question but my understanding of how constructors and destructors pertain to built in types is too shaky for me to interpret it usefully. Could you elaborate? – Jonathan Mee Jul 07 '16 at 17:47

3 Answers3

1

Yes, int[2] is a trivial type. From basic.types/9

... Scalar types, trivial class types (Clause [class]), arrays of such types and cv-qualified versions of these types ([basic.type.qualifier]) are collectively called trivial types. ...

And int is a scalar type.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

So my question is: Is int[2] a "TrivialType"?

Yes, it is.

You can easily check this, all the necessary tools are right at hand from <type_traits>:

#include <type_traits>
#include <iostream>

int main() {
    int a[2];

    std::cout << "a is " << (std::is_trivial<decltype(a)>()?"":"non ") << "trivial" << std::endl;
}

outputs

a is trivial

See the Live Demo

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Ha, I feel a little bit dumb now... I'm guessing you didn't mean to include `iomanip`? – Jonathan Mee Jul 07 '16 at 17:50
  • 1
    @JonathanMee _"I'm guessing you didn't mean to include iomanip?"_ Yes, I prematurely included it from the vision to output something like `true` or `false` and need to use `std::boolalpha`. I've seen much, much dumber questions here already BTW, don't worry. – πάντα ῥεῖ Jul 07 '16 at 17:56
1

A int is a Arithmetic type per [basic.fundamental] and a Arithmetic type is also called a scalar type per [basic.types]/9

Arithmetic types (3.9.1), enumeration types, pointer types, pointer to member types (3.9.2), std::nullptr_-t, and cv-qualified versions of these types (3.9.3) are collectively called scalar types.

and then we have

Scalar types, trivial class types (Clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called trivial types

emphasis mine

So an scalar type and an array of a scalar are both trivial types.

All quotes from draft N3797

NathanOliver
  • 171,901
  • 28
  • 288
  • 402