SFINAE of incomplete types
My head will explode.
I don't find anything wrong in the following code.
Output must be:
but it is:
What is wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <vector>
#include <array>
using namespace std;
//! Resizable for non-resizable containers
template<class T, class T1 = void> struct container_resizable { constexpr static bool value = false; };
//! Resizable for resizable containers
template<class T> struct container_resizable<T, typename enable_if<
is_same<T, vector<typename T::value_type, typename T::allocator_type>>::value /* ||
is_same<T, my_vector<typename T::value_type>>::value */
, T>::type>
{ constexpr static bool value = true; };
int main()
{
cout << container_resizable<vector<int>>::value << endl;
cout << container_resizable<array<int,5>>::value << endl;
return 0;
}
|
Complete rewrite of code.
It is working now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <vector>
#include <array>
using namespace std;
template<typename T>
struct container_resizable
{
constexpr static bool value = is_same<T, vector<typename T::value_type>>::value;
};
int main()
{
cout << container_resizable<vector<int>>::value << endl;
cout << container_resizable<array<int,5>>::value << endl;
return 0;
}
|
Using SFINAE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <type_traits>
#include <vector>
#include <list>
#include <array>
#include <iostream>
template < typename CONTAINER, typename = void >
struct container_resizable : std::false_type {};
template < typename CONTAINER >
struct container_resizable< CONTAINER, decltype( CONTAINER().resize(0) ) >
: std::true_type {};
int main()
{
std::cout << std::boolalpha
<< container_resizable< std::vector<int> >::value << '\n' // true
<< container_resizable< std::list<int> >::value << '\n' // true
<< container_resizable< std::array<int,5> >::value << '\n' // false
<< container_resizable< int[23] >::value << '\n' ; // false
}
|
http://liveworkspace.org/code/3p6Jfk$2
Topic archived. No new replies allowed.