Boost logo

Boost :

Subject: [boost] [typeof] GCC error: expression not defined in this scope
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2011-02-25 11:33:45


On Mon, Feb 7, 2011 at 5:29 PM, Steven Watanabe <watanabesj_at_[hidden]> wrote:
> On 2/7/2011 1:52 PM, Lorenzo Caminiti wrote:
>>
>>     void *result_ptr; // Plus append __LINE__...
>>     typedef typeof(*result_ptr) result_type;
>
> That's almost what we did.
>
> void deduce_result();
> typedef boost::function_traits<
>  typeof(&deduce_result)>::result_type result_type;
>
> is a little better.  (It can handle references.)

Hello all,

Why the following TYPEOF code does not compile on GCC but it does on
MSVC? Is this a GCC bug? If so, is there a workaround for it?

#include <boost/typeof/typeof.hpp>

int f() { return -1; }

template<typename T>
void g(T x) {
    int (deduce_func)();
    typedef BOOST_TYPEOF_TPL(deduce_func) func_type; // error also if
TYPEOF is used instead of TYPEOF_TPL

    struct s {
        void call(func_type func) { func(); } // line 12
    } ss;
    ss.call(f); // line 14
}

int main() {
    g(1); // line 18
    return 0;
}

$ g++ -Wall -Werror -I../../.. r03.cpp
r03.cpp: In member function ‘void g(T)::s::call(__typeof__
(boost::type_of::ensure_obj(deduce_func))) [with T = int]’:
r03.cpp:14: instantiated from ‘void g(T) [with T = int]’
r03.cpp:18: instantiated from here
r03.cpp:12: error: ‘deduce_func’ was not declared in this scope

What is this error? I don't understand it because with the local
struct at line 12 I am just accessing the type generated by the
typedef and not the actual expression that was used to deduce such a
type...

$ g++ --version
g++ (GCC) 4.3.4 20090804 (release) 1

The same error raises even also if TYPEOF is used instead of
TYPEOF_TPL. However, no error if g() is not a template so GCC compiles
this just fine:

#include <boost/typeof/typeof.hpp>

int f() { return -1; }

void g(int x) { // no template, no error -- why?
    int (deduce_func)();
    typedef BOOST_TYPEOF(deduce_func) func_type;

    struct s {
        void call(func_type func) { func(); }
    } ss;
    ss.call(f);
}

int main() {
    g(1);
    return 0;
}

Both examples (template and not) compile instead just fine on MSVC. Do
these examples compile on compilers other than GCC and MVSC?

Thank you very much!

-- 
Lorenzo

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk