Re: SFINAE, specialization, trait selection and problems
As is so often the case, I thought of a solution before the question got
through moderation (posted below.) This is much cleaner, and works without
#ifdef's. It still fails the Comeau online test, but I think that's because
of it either not linking or instatiating also those paths that should fail.
I'm pretty certain it can be improved upon further.
However, I'm still wondering what was wrong in my first attempt.
template <typename T>
struct random_access_container<T, std::random_access_iterator_tag>
:
#ifdef __INTEL_COMPILER
private fundamental_type_trait<typename T::value_type>,
#endif
private string_type_trait<T, is_string<T>::yes>
{
#ifdef __GNUC__
static fundamental_type_trait<typename T::value_type>
value_type_is_not_fundamental_integer_type;
#endif
public:
using string_type_trait<T, is_string<T>::yes>::name;
};
I believe that both the ICC and GCC solutions should work on both, but as it
turned out, they were mutually exclusive. Am I wrong, or have I found two
different bugs in two compilers with one code snippet?
_
/Bjorn
// new better version
#include <string>
#include <iostream>
#include <ostream>
#include <limits>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
static const char* names[] = { "string", "fundamental", "random access
container" };
typedef enum { string, fundamental, rac } name;
template <typename T>
class is_container
{
private:
typedef char one;
struct two
{
char c[2];
};
template <typename N>
static one test(typename N::iterator*);
template <typename N>
static two test(...);
public:
enum { yes = (sizeof(test<T>(0)) == sizeof(one)) };
enum { no = !yes };
};
template <typename T>
class is_string
{
private:
typedef char one;
struct two
{
char c[2];
};
template <typename N, typename O, typename P>
static one test(std::basic_string<N,O,P>*);
static two test(...);
public:
enum { yes = (sizeof(test((T*)0)) == sizeof(one)) };
enum { no = !yes };
};
template <bool b, typename T1, typename T2>
class if_else_t;
template <typename T1, typename T2>
class if_else_t<true, T1, T2>
{
public:
typedef T1 type;
};
template <typename T1, typename T2>
class if_else_t<false, T1, T2>
{
public:
typedef T2 type;
};
template <typename T>
struct type_must_be_fundamental_integer_or_random_access_container_of_such;
template <typename T, bool f = ( std::numeric_limits<T>::is_integer
&& std::numeric_limits<T>::is_specialized)>
struct fundamental_type_trait
{
static const int name = fundamental;
};
template <typename T>
struct fundamental_type_trait<T, false>
: public
type_must_be_fundamental_integer_or_random_access_container_of_such<T>
{
};
template <typename T, typename tag>
class typed_container_trait
: public
type_must_be_fundamental_integer_or_random_access_container_of_such<T>
{
};
template <typename T>
class container_trait : public typed_container_trait<T, typename
T::iterator::iterator_category>
{
};
template <typename T>
struct type_trait : public if_else_t<is_container<T>::yes,
container_trait<T>,
fundamental_type_trait<T> >::type
{
};
template <typename>
struct string_type_trait
{
static const int name = string;
};
template <typename T>
struct random_access_container_trait
: private fundamental_type_trait<typename T::value_type>
{
static const int name = rac;
};
template <typename T>
struct typed_container_trait<T, std::random_access_iterator_tag>
: public if_else_t<is_string<T>::yes,
string_type_trait<T>,
random_access_container_trait<T> >::type
{
};
template <typename T>
void print(const char *type)
{
std::cout << type << " is " << names[type_trait<T>::name] << std::endl;
}
#define PRINT(t) print<t>(#t)
int main(void)
{
// These should all work.
PRINT(int);
PRINT(std::vector<int>);
PRINT(std::wstring);
PRINT(std::deque<int>);
PRINT(std::string);
// These must cause compilation errors if uncommented.
//PRINT(std::vector<std::string>);
// PRINT(std::list<int>);
// PRINT(double);
// PRINT(name);
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]