Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <pre><code>// IsIterator.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include &lt;iterator&gt; #include &lt;iostream&gt; #include &lt;vector&gt; template &lt;typename T&gt; class ExtractType { public: typedef T Result; }; template &lt;typename U&gt; class ExtractType&lt;std::iterator_traits&lt;U&gt; &gt; { public: typedef U Result; }; template &lt;typename T&gt; class IsIteratorPointer { public: static const bool value = false; }; template &lt;typename T&gt; class IsIteratorPointer&lt;T*&gt; { public: static const bool value = true; }; template &lt;typename T&gt; class IsIteratorPointer&lt;const T*&gt; { public: static const bool value = true; }; template &lt;typename T&gt; class IsIterator { public: struct TrueValue { char val; }; struct FalseValue { char val[2]; }; template &lt;typename U&gt; static TrueValue evaluateIsIter(typename U::iterator_category*); template &lt;typename U&gt; static FalseValue evaluateIsIter(...); typedef typename ExtractType&lt;T&gt;::Result TestType; static const bool value = IsIteratorPointer&lt;TestType&gt;::value || sizeof(evaluateIsIter&lt;TestType&gt;(0)) == sizeof(TrueValue); }; struct Foo { }; template &lt;&gt; struct std::iterator_traits&lt;Foo&gt; { typedef random_access_iterator_tag iterator_category; }; int _tmain(int argc, _TCHAR* argv[]) { std::cout &lt;&lt; IsIterator&lt; int &gt;::value &lt;&lt; std::endl; std::cout &lt;&lt; IsIterator&lt; std::iterator_traits&lt;int&gt; &gt;::value &lt;&lt; std::endl; std::cout &lt;&lt; IsIterator&lt; std::iterator_traits&lt;int*&gt; &gt;::value &lt;&lt; std::endl; std::cout &lt;&lt; IsIterator&lt; std::iterator_traits&lt;Foo&gt; &gt;::value &lt;&lt; std::endl; // Will be 0 (only drawback) - typedef must be in Foo directly std::cout &lt;&lt; IsIterator&lt; std::vector&lt;int&gt;::const_iterator &gt;::value &lt;&lt; std::endl; return 0; } </code></pre> <p>The question now remains: Can this drawback somehow be removed?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload