Eric Fiselier da1818a08c [libc++] Try and prevent evaluation of is_default_constructible on tuples default constructor if it is not needed.
Summary:
Currently parts of the SFINAE on tuples default constructor always gets evaluated even when the default constructor is never called or instantiated. This can cause a hard compile error when a tuple is created with types that do not have a default constructor. Below is a self contained example using a pair like class. This code will not compile but probably should.

```

#include <type_traits>

template <class T>
struct IllFormedDefaultImp {
    IllFormedDefaultImp(T x) : value(x) {}
    constexpr IllFormedDefaultImp() {}
    T value;
};

typedef IllFormedDefaultImp<int &> IllFormedDefault;

template <class T, class U>
struct pair
{
  template <bool Dummy = true,
    class = typename std::enable_if<
         std::is_default_constructible<T>::value
      && std::is_default_constructible<U>::value
      && Dummy>::type
    >
  constexpr pair() : first(), second() {}

  pair(T const & t, U const & u) : first(t), second(u) {}

  T first;
  U second;
};

int main()
{
  int x = 1;
  IllFormedDefault v(x);
  pair<IllFormedDefault, IllFormedDefault> p(v, v);
}
```

One way to fix this is to use `Dummy` in a more involved way in the constructor SFINAE. The following patch fixes these sorts of hard compile errors for tuple.


Reviewers: mclow.lists, rsmith, K-ballo, EricWF

Reviewed By: EricWF

Subscribers: ldionne, cfe-commits

Differential Revision: http://reviews.llvm.org/D7569

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@230120 91177308-0d34-0410-b5e6-96231b3b80d8
2015-02-21 02:30:41 +00:00

111 lines
3.0 KiB
C++

//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <tuple>
// template <class... Types> class tuple;
// constexpr tuple();
// UNSUPPORTED: c++98, c++03
#include <tuple>
#include <string>
#include <cassert>
#include <type_traits>
#include "DefaultOnly.h"
struct NoDefault {
NoDefault() = delete;
explicit NoDefault(int) { }
};
struct NoExceptDefault {
NoExceptDefault() noexcept = default;
};
struct ThrowingDefault {
ThrowingDefault() { }
};
struct IllFormedDefault {
IllFormedDefault(int x) : value(x) {}
template <bool Pred = false>
constexpr IllFormedDefault() {
static_assert(Pred,
"The default constructor should not be instantiated");
}
int value;
};
int main()
{
{
std::tuple<> t;
}
{
std::tuple<int> t;
assert(std::get<0>(t) == 0);
}
{
std::tuple<int, char*> t;
assert(std::get<0>(t) == 0);
assert(std::get<1>(t) == nullptr);
}
{
std::tuple<int, char*, std::string> t;
assert(std::get<0>(t) == 0);
assert(std::get<1>(t) == nullptr);
assert(std::get<2>(t) == "");
}
{
std::tuple<int, char*, std::string, DefaultOnly> t;
assert(std::get<0>(t) == 0);
assert(std::get<1>(t) == nullptr);
assert(std::get<2>(t) == "");
assert(std::get<3>(t) == DefaultOnly());
}
{
// See bug #21157.
static_assert(!std::is_default_constructible<std::tuple<NoDefault>>(), "");
static_assert(!std::is_default_constructible<std::tuple<DefaultOnly, NoDefault>>(), "");
static_assert(!std::is_default_constructible<std::tuple<NoDefault, DefaultOnly, NoDefault>>(), "");
}
{
static_assert(noexcept(std::tuple<NoExceptDefault>()), "");
static_assert(noexcept(std::tuple<NoExceptDefault, NoExceptDefault>()), "");
static_assert(!noexcept(std::tuple<ThrowingDefault, NoExceptDefault>()), "");
static_assert(!noexcept(std::tuple<NoExceptDefault, ThrowingDefault>()), "");
static_assert(!noexcept(std::tuple<ThrowingDefault, ThrowingDefault>()), "");
}
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
{
constexpr std::tuple<> t;
}
{
constexpr std::tuple<int> t;
assert(std::get<0>(t) == 0);
}
{
constexpr std::tuple<int, char*> t;
assert(std::get<0>(t) == 0);
assert(std::get<1>(t) == nullptr);
}
{
// Check that the SFINAE on the default constructor is not evaluted when
// it isn't needed. If the default constructor is evaluted then this test
// should fail to compile.
IllFormedDefault v(0);
std::tuple<IllFormedDefault> t(v);
}
#endif
}