Me or compiler? 0x

Discussion in 'C++' started by Noah Roberts, Jan 3, 2011.

  1. Noah Roberts

    Noah Roberts Guest

    Input:

    template < typename T >
    boost::function<void()> f()
    {
    return []() { typedef typename T::type type; };
    }

    Output: "typename cannot be used outside a template declaration",
    "'T' : is not a class or namespace name"

    Input:

    template < typename T >
    boost::function<void()> f()
    {
    return []() { typedef T::type type; };
    }

    Output: "'T' : is not a class or namespace name"

    Input:

    template < typename T >
    struct wtf { typedef typename T::type type; };

    template < typename T >
    boost::function<void()> f()
    {
    return []() { typedef typename wtf<T>::type type; };
    }

    Output: "typename cannot be used outside a template declaration"

    Input:

    template < typename T >
    struct wtf { typedef typename T::type type; };

    template < typename T >
    boost::function<void()> f()
    {
    return []() { typedef wtf<T>::type type; };
    }

    Output: success (assuming T has a type in it)

    So the question is, am I doing something wrong or is the compiler just
    confused? What of these is correct?


    --
    http://crazyeddiecpp.blogspot.com/
    Noah Roberts, Jan 3, 2011
    #1
    1. Advertising

  2. Noah Roberts

    SG Guest

    On 4 Jan., 00:45, Noah Roberts wrote:
    > Input:
    >
    > template < typename T >
    > boost::function<void()> f()
    > {
    >   return []() { typedef typename T::type type; };
    > }
    >
    > Output:  "typename cannot be used outside a template declaration",
    > "'T' : is not a class or namespace name"


    I also would have expected it to work. G++ 4.5.1 compiles the
    following code:

    template<class T>
    void f() {
    []() { typedef typename T::type type; }();
    }

    struct foo {
    typedef int type;
    };

    int main() {
    f<foo>();
    }

    So, I guess that (1) you're using the Microsoft Compiler and (2) you
    encountered a compiler bug.

    Have you tried the following?

    template < typename T >
    boost::function<void()> f()
    {
    typedef typename T::type T_type;
    return []() { typedef T_type type; };
    }


    Cheers!
    SG
    SG, Jan 4, 2011
    #2
    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. Yan
    Replies:
    0
    Views:
    1,114
  2. Jack Wright
    Replies:
    5
    Views:
    611
    Shiv Kumar
    Jan 19, 2004
  3. Ram
    Replies:
    0
    Views:
    2,814
  4. Andrey Batyuck

    Compiler compiler with C++ as output

    Andrey Batyuck, May 11, 2004, in forum: C++
    Replies:
    3
    Views:
    428
    Frederik Hertzum
    May 17, 2004
  5. RickMuller
    Replies:
    4
    Views:
    692
    Alexey Shamrin
    Mar 26, 2005
Loading...

Share This Page