Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does is_constructible claim something is constructible when it isn't?

The following program, when compiled with either GCC 4.7 and clang 3.2, produces "1" as output.

#include <type_traits>

struct foo {
    template<typename T>
    foo(T) {
        static_assert(not std::is_same<int, T>(), "no ints please");
    }
};

#include <iostream>    
int main() {
    std::cout << std::is_constructible<foo, int>();
}

This is confusing. foo is quite clearly not constructible from int! If I change main to the following, both compilers reject it due to the static assertion failing:

int main() {
    foo(0);
}

How come both compilers say it is constructible?

like image 466
R. Martinho Fernandes Avatar asked Feb 11 '13 16:02

R. Martinho Fernandes


Video Answer


2 Answers

foo2 is your foo. foo1 is a foo that does what you want your foo to do.

#include <type_traits>
#include <utility>

struct foo1 {
  template<typename T,typename=typename std::enable_if< !std::is_same<int, T>::value >::type>
  foo1(T) {
    static_assert(not std::is_same<int, T>(), "no ints please");
  }
};
struct foo2 {
  template<typename T>
  foo2(T) {
    static_assert(not std::is_same<int, T>(), "no ints please");
  }
};

#include <iostream>    
int main() {
  std::cout << std::is_constructible<foo1, int>();
  std::cout << std::is_constructible<foo2, int>();
}
like image 22
Yakk - Adam Nevraumont Avatar answered Sep 29 '22 11:09

Yakk - Adam Nevraumont


This is what the standard has to say (§20.9.5/6), with my emphasis:

Given the following function prototype:

template <class T>
typename add_rvalue_reference<T>::type create();

the predicate condition for a template specialization is_constructible<T, Args...> shall be satisfied if and only if the following variable definition would be well-formed for some invented variable t:

T t(create<Args>()...);

[ Note: These tokens are never interpreted as a function declaration. —end note ]

Access checking is performed as if in a context unrelated to T and any of the Args. Only the validity of the immediate context of the variable initialization is considered. [ Note: The evaluation of the initialization can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the “immediate context” and can result in the program being ill-formed. —end note ]

The assertion only fails when the template constructor is instantiated. However, as cleared up in the note, that assertion is not in the immediate context of the variable definition that is considered, and thus does not affect its "validity". So the compilers can count that definition as valid, and thus claim that foo is indeed constructible from int, even if actually attempting to construct a foo from an int results in an ill-formed program.

Note that the compilers are also allowed to, instead of having is_constructible yield false, just reject the original program based on the assertion, even though neither one does.

like image 143
R. Martinho Fernandes Avatar answered Sep 29 '22 11:09

R. Martinho Fernandes