473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

SFINAE problem.

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
Sep 26 '05 #1
4 1616
In article <0h********************@wagner.videotron.net>,
"christopher diggins" <cd******@videotron.ca> 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
Sep 26 '05 #2
>> 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
Sep 26 '05 #3
In article <TX********************@wagner.videotron.net>,
"christopher diggins" <cd******@videotron.ca> 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
Sep 26 '05 #4
"Howard Hinnant" <ho************@gmail.com> wrote in message
news:ho**********************************@syrcnyrd rs-02-ge0.nyroc.rr.com...
[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
Sep 26 '05 #5

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

Similar topics

8
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...
3
by: justin.adam.miller | last post by:
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...
2
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...
4
by: kaalus | last post by:
Is it possible to use SFINAE to provide different implementations of some function depending on the fact that operator << is overloaded for some type? For example: template<class T> void...
1
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...
6
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...
5
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...
2
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...
35
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.