Problem compiling template function with typename for dependent names

Discussion in 'C++' started by antoniogarcar@gmail.com, Nov 20, 2012.

  1. Guest

    Hi all,

    I have a short question. The following program does not compile using g++4.6.3, ¿why?


    #include <list>

    template <class T>
    void empty (typename std::list<T>::iterator it)
    {}

    int main()
    {
    std::list<int> l;
    empty(l.begin());
    }
    , Nov 20, 2012
    #1
    1. Advertising

  2. SG Guest

    Re: Problem compiling template function with typename for dependentnames

    Am 20.11.2012 09:33, schrieb :
    >
    > The following program does not compile using g++4.6.3, ¿why?
    >
    > #include <list>
    >
    > template <class T>
    > void empty (typename std::list<T>::iterator it)


    - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    This is a non-deducible context. Or, as Stephan T. Lavavej would say
    "The double colon between T and 'iterator' prevents the compiler from
    deducing T" (or something like that).

    > {}
    >
    > int main()
    > {
    > std::list<int> l;
    > empty(l.begin());
    > }


    Here, the compiler won't deduce the template parameter T as a general
    rule. You can't expect the compiler to try every possible T and see what
    fits. There are other cases where there is no "solution" for T:

    template<class T>
    struct foo {
    typedef int blah;
    }

    template<class T>
    void empty(typename foo<T>::blah);

    int main()
    {
    empty(42); // What's T ?!
    }

    HTH,
    SG
    SG, Nov 20, 2012
    #2
    1. Advertising


  3. > HTH,
    >
    > SG


    Thank you. That helps.
    Antonio Garrido, Nov 20, 2012
    #3
  4. Zhihao Yuan Guest

    On Tuesday, November 20, 2012 2:33:41 AM UTC-6, Antonio Garrido wrote:
    > template <class T>
    > void empty (typename std::list<T>::iterator it)
    > {}


    First, like SG said, a typename is not deducible. Second, operations
    work on a iterator should be generalized regarding to the *type* of
    the iterator, not the type of the value. Try this:

    template <typename It>
    void empty (It it, typename std::enable_if<
    std::is_convertible<
    typename std::iterator_traits<It>::iterator_category,
    std::bidirectional_iterator_tag>::value>::type* = 0) {
    // or forward_iterator_tag, etc.
    }
    Zhihao Yuan, Nov 20, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pervinder
    Replies:
    4
    Views:
    2,998
    Malte Starostik
    Mar 29, 2005
  2. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    353
    Rolf Magnus
    Mar 14, 2006
  3. Evan
    Replies:
    3
    Views:
    348
  4. Sanil
    Replies:
    2
    Views:
    4,748
    James Kanze
    Dec 14, 2007
  5. puzzlecracker
    Replies:
    1
    Views:
    518
    James Kanze
    Aug 7, 2008
Loading...

Share This Page