473,543 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SFINAE compilation troubles

I've been trying to use the sfinae principle in some code and have been
getting many compiler errors. So I decided to try a very simplified
version to see if I had the idea correct. Here's the example:

#include <iostream>

template <typename T>
class IsClassT
{
private:
typedef char One;
typedef struct { char a[2]; } Two;
template <typename C> static One test(int C::*);
template <typename C> static Two test(...);
public:
enum { Yes = sizeof(IsClassT <T>::test<T>(0) ) == 1 };
enum { No = !Yes };
};

class A
{
public:
int a;
};

int main()
{
using namespace std;

if(IsClassT<A>: :Yes)
cout << "A is a class!" << endl;
if(IsClassT<A*> ::Yes)
cout << "A* is a class too!" << endl;

return 0;
}

This you may recognize is copied DIRECTLY from "C++ Templates" by
Josuttis, at least the template part anyway.

This is supposed to work right? Why does it think it can't find the
"test" function? It looks like the compiler isn't matching either of
those overloads. Also, as neither of those "test" functions are
actually being called (sizeof doesn't actually call the function), code
doesn't need to be provided for them.

Please, any help would be GREATLY appreciated!! Posted below are the
error messages I got when compiling with g++ 3.2.2 and 3.4.2.

With g++ 3.2.2

sfinae.cxx:5: warning: all member functions in class `IsClassT<T>' are
private
sfinae.cxx: In instantiation of `IsClassT<A>':
sfinae.cxx:26: instantiated from here
sfinae.cxx:26: no method `IsClassT<A>::t est<A>'
sfinae.cxx:26: enumerator value for `Yes' not integer constant
sfinae.cxx: In instantiation of `IsClassT<A*>':
sfinae.cxx:28: instantiated from here
sfinae.cxx:28: no method `IsClassT<A*>:: test<A*>'
sfinae.cxx:28: enumerator value for `Yes' not integer constant

With g++ 3.4.2:

sfinae.cxx:12: error: expected primary-expression before '>' token
sfinae.cxx: In instantiation of `IsClassT<A>':
sfinae.cxx:26: instantiated from here
sfinae.cxx:12: error: enumerator value for `Yes' not integer constant
sfinae.cxx: In instantiation of `IsClassT<A*>':
sfinae.cxx:28: instantiated from here
sfinae.cxx:12: error: enumerator value for `Yes' not integer constant

Jul 23 '05 #1
3 1872
ju************* ***@gmail.com wrote:
[SNIP]
template <typename T>
class IsClassT
{
private:
typedef char One;
typedef struct { char a[2]; } Two;
template <typename C> static One test(int C::*);
template <typename C> static Two test(...);
public:
enum { Yes = sizeof(IsClassT <T>::test<T>(0) ) == 1 };
enum { No = !Yes };
}; [SNIP] Josuttis, at least the template part anyway.

[SNIP]

I was able to "fix" it this way:
enum { Yes = sizeof(IsClassT <T>::template test<T>(0)) == 1 };
Note the template keyword before the test function name. I am not sure it
is needed there, I have guessed that somehow g++ does not understand that
IsClassT<T>::te st is a template name.

Comeau online compiles it both ways, which is weird. I suggest you look
around on the books' web for the errata, you may find a comment there
explaining the problem of g++.

Attila
Jul 23 '05 #2
Thanks so much for the response.

Your fix does indeed compile fine with 3.4.2, but not with 3.2.2. Is
this simply a compiler bug that was fixed somewhere between
3.2.2-3.4.2?

Oddly enough, that code now compiles on the Sun compiler v6U2. It
doesn't work according to the standard however. IsClassT<A>::Ye s
returns false. It seems to match the (...) version of test before the
one with the pointer to a member. Not the first bug I've found in Sun's
compiler (at least the earlier versions anyway).

Justin

Jul 23 '05 #3
ju************* ***@gmail.com wrote:
Thanks so much for the response.

Your fix does indeed compile fine with 3.4.2, but not with 3.2.2. Is
this simply a compiler bug that was fixed somewhere between
3.2.2-3.4.2?
I have no idea. :-) I am inclined to suspect that it is rather 3.4.2 which
is at fault, but I only base my quess on what Comeau does in strict mode.
Of course this can be some sort of corner case, as Comeau accepts both forms
(the one with and without the template keyword).

As for the differences betweem 3.2.2 and 3.4.2, I think (but I may be wrong)
that 3.4.2 has a new C++ parser and has also introduced two phase name
lookup for templates. Any of those two is good enough cause for such a
change in behavior.
Oddly enough, that code now compiles on the Sun compiler v6U2. It
doesn't work according to the standard however. IsClassT<A>::Ye s
returns false. It seems to match the (...) version of test before the
one with the pointer to a member. Not the first bug I've found in
Sun's compiler (at least the earlier versions anyway).


What I know as error in v6u2 was an error introduced in the 6 series, a name
mangling problem with function signatures containing const arguments passed
by value. The template support is better with v6, but it has its little
problems, so some things just won't compile (or work).

--
Attila aka WW
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2686
by: Peter Collingbourne | last post by:
Hello I am trying to do some template metaprogramming involving enabling or disabling a method in a parameterised class based on some condition. I am trying to use the Boost enable_if mechanism to do this, using the SFINAE principle. Here is a simple example showing what I am trying to do: 1 #include <iostream.h> 2 #include...
2
3443
by: Clark S. Cox III | last post by:
I'm writing a class that, depending on a template parameter, has constructors that take differing numbers of arguments. I initially thought that I could use SFINAE (via boost::enable_if_c) to achieve my ends, but I have hit a snag. If I've messed up somewhere, or there's another way to get what I'm after, I'd be greaful if someone could...
1
1813
by: Dilip | last post by:
I am a little confused about the difference between SFINAE and unambiguous overload resolution set. For ex: template<typename Tvoid func(T); template<typename Tvoid func(T*); now, func<intis going to be ambiguous. Ok so far so good. Now:
6
2927
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the raw pointer at "the end of the arrow". For types that support the operator, I have a reasonable solution, but I'd like to have a null pointer...
3
424
by: none | last post by:
I am trying to understand SFINAE based on this wiki page: http://en.wikipedia.org/wiki/SFINAE // Defines return type to be void for all types // besides int. template<typename T> struct can_use_f {
5
2064
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not issue any error. If I replace U with U(*), then the code compiles again. I couldn't make the sense out of it. What's the magic with (*)? Note that...
2
2453
by: Barry | last post by:
The problem brought by one earlier post from comp.lang.c++ http://groups.google.com/group/comp.lang.c++/browse_thread/thread/bf636c48b84957b/ I take part of the question and reproduce the code to represent (partly) the question. #include <iostream> template <class> void f(...)
35
1961
by: James Kanze | last post by:
Just ran into an interesting question concerning SFINAE. Given the following code: #include <iostream> #include <typeinfo> template< typename T > class P { public:
0
2891
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit instantiation of the partially specialized version, but my compiler (VC8) complains because it fails the SFINAE version. I just realized as I was typing...
0
7397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7582
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7675
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5877
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5257
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3385
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
948
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
626
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.