Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > SFINAE problem.

Reply
Thread Tools

SFINAE problem.

 
 
christopher diggins
Guest
Posts: n/a
 
      09-26-2005
I am trying to make compile time decisions based on whether a struct defines
a member type. It seems to be a job for SFNIAE but I am struggling:

#include <iostream>
#include <cstdlib>

using namespace std;

struct fu { };
struct bar { typedef int type; };

template<typename T, typename U = void>
struct is_true {
static const bool value = false;
};

template<typename T>
struct is_true<T, typename T::type> {
static const bool value = true;
};

int main() {
bool b;
b = is_true<fu>::value;
cout << b << endl;
b = is_true<bar>::value;
cout << b << endl;
system("pause");
};

However, this outputs:
0
0

I am looking at the following:
http://boost-consulting.com/boost/li...enable_if.html

But it doesn't seem to do what it is that I specifically need.
Any help would be greatly appreciated. TIA!

--
Christopher Diggins
http://www.cdiggins.com


 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      09-26-2005
In article <0hVZe.23741$(E-Mail Removed)>,
"christopher diggins" <(E-Mail Removed)> wrote:

> I am trying to make compile time decisions based on whether a struct defines
> a member type. It seems to be a job for SFNIAE but I am struggling:
>
> #include <iostream>
> #include <cstdlib>
>
> using namespace std;
>
> struct fu { };
> struct bar { typedef int type; };
>
> template<typename T, typename U = void>
> struct is_true {
> static const bool value = false;
> };
>
> template<typename T>
> struct is_true<T, typename T::type> {
> static const bool value = true;
> };
>
> int main() {
> bool b;
> b = is_true<fu>::value;
> cout << b << endl;
> b = is_true<bar>::value;
> cout << b << endl;
> system("pause");
> };
>
> However, this outputs:
> 0
> 0


Your code prints:

0
1

for me (CodeWarrior Pro 10 (beta)).

-Howard
 
Reply With Quote
 
 
 
 
christopher diggins
Guest
Posts: n/a
 
      09-26-2005
>> However, this outputs:
>> 0
>> 0

>
> Your code prints:
>
> 0
> 1
>
> for me (CodeWarrior Pro 10 (beta)).



Thanks for checking that out. So I am not entirely a bumbling idiot
I am using GCC 3.4.2. So, in theory is what I am doing here correct?

--
Christopher Diggins
http://www.cdiggins.com


 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      09-26-2005
In article <TXVZe.23745$(E-Mail Removed)>,
"christopher diggins" <(E-Mail Removed)> wrote:

> >> However, this outputs:
> >> 0
> >> 0

> >
> > Your code prints:
> >
> > 0
> > 1
> >
> > for me (CodeWarrior Pro 10 (beta)).

>
>
> Thanks for checking that out. So I am not entirely a bumbling idiot
> I am using GCC 3.4.2. So, in theory is what I am doing here correct?


Well I was about to say yes, but now I'm not so sure. I converted your
example to a compile time test and ran it on Comeau (thanks Greg!):

struct fu { };
struct bar { typedef int type; };

template<typename T, typename U = void>
struct is_true {
static const bool value = false;
};

template<typename T>
struct is_true<T, typename T::type> {
static const bool value = true;
};

int main() {
char test1[!is_true<fu>::value];
char test2[is_true<bar>::value];
};

and it failed there.

Assuming I understand what you're trying to do, try this instead:

template <class T>
struct has_type
{
private:
struct two {char x; char y;};
template <class U> static two test(...);
template <class U> static char test(typename U::type* = 0);
public:
static const bool value = sizeof(test<T>(0)) == 1;
};

struct fu { };
struct bar { typedef int type; };

int main() {
char test1[!has_type<fu>::value];
char test2[has_type<bar>::value];
}

That passes for me everywhere I tried it (including gcc 4.0).

-Howard
 
Reply With Quote
 
christopher diggins
Guest
Posts: n/a
 
      09-26-2005
"Howard Hinnant" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
[snip]
> That passes for me everywhere I tried it (including gcc 4.0).


That is wonderful Howard, it works great on GCC 3.4.2, thank you very much!

--
Christopher Diggins
http://www.cdiggins.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
confused with SFINAE Dilip C++ 1 07-06-2006 09:35 PM
SFINAE kaalus@gmail.com C++ 4 04-26-2006 02:08 PM
Using SFINAE with constructors Clark S. Cox III C++ 2 09-09-2005 09:34 PM
SFINAE compilation troubles justin.adam.miller@gmail.com C++ 3 02-08-2005 08:47 AM
problem with SFINAE applied to class methods Peter Collingbourne C++ 8 07-04-2004 07:25 PM



Advertisments