Does anyone have a workaround for this gcc4.3 issue?

H

huili80

Code is as follows. It's legal c++ code, however doesn't compile with g++4.3.
Would really appreciate if anyone has a workaround!! Thanks!

-----------------------------------------------

template < typename T > T& obj();

template < typename T, typename=typeof(obj<T>().memfun()) >
struct test {};

template < typename T > test<T> f(int){}

template <typename> char f(...){}

struct X
{
void memfun(int){};
};

int main()
{
f<X>(0);
}
 
A

Alf P. Steinbach

Code is as follows. It's legal c++ code, however doesn't compile with g++4.3.
Would really appreciate if anyone has a workaround!! Thanks!

-----------------------------------------------

template < typename T > T& obj();

template < typename T, typename=typeof(obj<T>().memfun()) >
struct test {};

template < typename T > test<T> f(int){}

template <typename> char f(...){}

struct X
{
void memfun(int){};
};

int main()
{
f<X>(0);
}

From the information given it's impossible to say what error g++ 4.3
produces.

See the FAQ item titled "How do I post a question about code that
doesn't work correctly?", currently available at (among others) <url:
http://www.parashift.com/c++-faq/posting-code.html), in particular the
three first bullet points.

It's often a good idea to take a look at the FAQ.

- - -

That said, the code isn't quite valid standard C++:

* In standard C++ there is no `typeof`. It's a g++ language extension.
C++11 does have a similar `decltype`.

* A non-void function must not return by the execution passing out of
the end of the function body, this is Undefined Behavior. Probably,
judging from the code, the functions are meant to be used in compile
time TMP machinery, and then just be unimplemented. But still.

Also, Visual C++ 12.0 refuses to accept the
"typename=typeof(obj<T>().memfun()", but as I recall it has a bug in
this area so it doesn't indicate any other problem than portability:
better rewrite if you want this to compile also with Visual C++.

- - -

Btw., what was the problem?

Can you re-post, according to the FAQ guidelines?


Cheers & hth.,

- Alf
 
H

huili80

Thanks Alf for looking at my post. Here is more information.

The problem is that this code compiles with gcc4.8 and above, as i've tested. however, unfortunately i do not currently have other compiler other than gcc4.3 (at work that is), and i hope to find a workaround for gcc4.3.

The non-compile appears to be a gcc4.3 issue (bug maybe?), where a substitution failure, i.e., the instantiation of test<X> when calling f<X>(0), is mistakenly being treated as an error.

As for the code itself, since i'm using gcc4.3 which doesn't have c++11, therefore no decltype, so i had to use the gcc extension typeof, which is really the only thing non-standard about it.

Thanks.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top