-1

Possible Duplicate:
enable_if iterator as a default template parameter?

I'm looking for solution to know at compile-time whether std::iterator_traits<T>::value_type is valid and defined. The problem with this is that std library forwards the declaration of value_type to the derived type in T:

typedef T::value_type value_type;

I need to know whether T::value_type is valid type or not in compile time to avoid errors related to value_type not existing.

Please consider these examples:

std::iterator_traits<int *>::value_type; // OK - should return that value_type exists as it's defined in specialization of std::iterator_traits

std::iterator_traits<const int *>::value_type; // OK - should return that value_type exists as it's defined in specialization of std::iterator_traits

std::iterator_traits<std::vector<int>::const_iterator> >::value_type; // OK - the value_type exists defined within std::vector<int>::const_iterator

std::iterator_traits<int>::value_type; // ERROR - the value_type is not defined within int class - this is what I'm trying to avoid to resolve the value_type of.

I need the solution to be fully compliant with C++ standard and std library standard and compiler independent.

8
  • 4
    This question I answered recently may be of help: stackoverflow.com/questions/11898657/… Aug 14, 2012 at 9:36
  • @R.MartinhoFernandes: That looks like a duplicate, in fact. Aug 14, 2012 at 9:38
  • I think whole problem with this is that it returns that the iterator_category exists: typedef typename _Iter::iterator_category iterator_category; defined in std::iterator_traits<T> default. Your sfinae will return that it exists, that the problem. Aug 14, 2012 at 9:43
  • @PavelCelba Did you try it? Unless _Iter::iterator_category exists, std::iterator_traits<T>::iterator_category results in substitution failure (and the same goes for value_type). Aug 14, 2012 at 9:45
  • Yes, it doesn't result in substitution failure as std::iterator_traits<T>::iterator_category is typedef. The compiler won't try to look whether the typedef is correct and substitution will be accepted. Aug 14, 2012 at 9:49

2 Answers 2

0

The problem can be shown with the approach in comments on this:

#include <iterator>
#include <iostream>

template <typename T>
class IsIterator {
public:
  struct TrueValue {
  char val;
 };

 struct FalseValue {
   char val[2];
 };

template <typename U>  
 static TrueValue evaluateIsIter(typename U::iterator_category*);

 template <typename U>
 static FalseValue evaluateIsIter(...);

 static const bool value = sizeof(evaluateIsIter<T>(0)) == sizeof(TrueValue);
};


int _tmain(int argc, _TCHAR* argv[])
{
std::cout << IsIterator< int >::value << std::endl;
std::cout << IsIterator< std::iterator_traits<int> >::value << std::endl;
return 0;
 }

With result on VS2005 compiler of: 0 1

On this can be seen that this is not enough to solve the problem. Or am I doing something wrong?

0

The solution is this, but with drawback that custom defined iterator classes must have typedefs defined inside the classes and not just specialized std::iterator_traits class.

// IsIterator.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iterator>
#include <iostream>
#include <vector>

template <typename T>
class ExtractType {
public:
  typedef T Result;  
};

template <typename U>
class ExtractType<std::iterator_traits<U> >
{
public:
  typedef U Result;
};

template <typename T>
class IsIteratorPointer
{
public:
  static const bool value = false;
};

template <typename T>
class IsIteratorPointer<T*>
{
public:
  static const bool value = true;
};

template <typename T>
class IsIteratorPointer<const T*>
{
public:
  static const bool value = true;
};

template <typename T>
class IsIterator {
public:
  struct TrueValue {
    char val;
  };

  struct FalseValue {
    char val[2];
  };

  template <typename U>  
  static TrueValue evaluateIsIter(typename U::iterator_category*);

  template <typename U>
  static FalseValue evaluateIsIter(...);

  typedef typename ExtractType<T>::Result TestType;

  static const bool value = IsIteratorPointer<TestType>::value || sizeof(evaluateIsIter<TestType>(0)) == sizeof(TrueValue);
};

struct Foo {  
};

template <> 
struct std::iterator_traits<Foo>
{
  typedef random_access_iterator_tag iterator_category;
};

int _tmain(int argc, _TCHAR* argv[])
{
  std::cout << IsIterator< int >::value << std::endl;
  std::cout << IsIterator< std::iterator_traits<int> >::value << std::endl;
  std::cout << IsIterator< std::iterator_traits<int*> >::value << std::endl;
  std::cout << IsIterator< std::iterator_traits<Foo> >::value << std::endl; // Will be 0 (only drawback) - typedef must be in Foo directly
  std::cout << IsIterator< std::vector<int>::const_iterator >::value << std::endl;

    return 0;
}

The question now remains: Can this drawback somehow be removed?

Not the answer you're looking for? Browse other questions tagged or ask your own question.