473,325 Members | 2,805 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.

problem with SFINAE applied to class methods

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 <boost/utility/enable_if.hpp>
3
4 using namespace boost;
5
6 template <bool CanFoo> class A {
7
8 public:
9 typename enable_if_c<CanFoo>::type foo() {
10 cout << "Foo" << endl;
11 }
12
13 };
14
15 int main(int argc, char** argv) {
16 A<false> cant_foo;
17 cant_foo.foo();
18 A<true> can_foo;
19 can_foo.foo();
20 }

In this example the foo method in the A class is supposed to only exist
when the boolean template parameter is true, thus I expect only one
compiler error to occur on line 17. However when compiling this code I
get the following additional message from the compiler (gcc 3.2.2):

traits.cc: In instantiation of `A<false>':
traits.cc:16: instantiated from here
traits.cc:9: no type named `type' in `struct boost::enable_if_c<false, void>'

Obviously there is not meant to be a type 'type' in this struct, which
is what SFINAE is all about! Is this a bug in the compiler or am I
misunderstanding the principle? If so can someone suggest an alternative
way to do what I want?

Thanks
--
Peter
Jul 22 '05 #1
8 2671
On Thu, 1 Jul 2004 16:31:42 +0000 (UTC), Peter Collingbourne
<pc***@doc.ic.ac.uk> wrote:
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 <boost/utility/enable_if.hpp>
3
4 using namespace boost;
5
6 template <bool CanFoo> class A {
7
8 public:
9 typename enable_if_c<CanFoo>::type foo() {
10 cout << "Foo" << endl;
11 }
12
13 };
14
15 int main(int argc, char** argv) {
16 A<false> cant_foo;
17 cant_foo.foo();
18 A<true> can_foo;
19 can_foo.foo();
20 }

In this example the foo method in the A class is supposed to only exist
when the boolean template parameter is true, thus I expect only one
compiler error to occur on line 17. However when compiling this code I
get the following additional message from the compiler (gcc 3.2.2):

traits.cc: In instantiation of `A<false>':
traits.cc:16: instantiated from here
traits.cc:9: no type named `type' in `struct boost::enable_if_c<false,
void>'

Obviously there is not meant to be a type 'type' in this struct, which
is what SFINAE is all about! Is this a bug in the compiler or am I
misunderstanding the principle? If so can someone suggest an alternative
way to do what I want?

Thanks


I think you are misunderstanding the principle. SNIFAE only applies
(AFAIK) to choice between alternative function templates (including member
function templates). When choosing the function template the fact that one
choice leads to a type error is not a problem, it just means that choice
is not made.

The following would seem to do what you want without using SNIFAE (or
enable_if)

#include <iostream>
#include <boost/static_assert.hpp>

using namespace boost;

template <bool CanFoo>
class A {

public:
void foo() {
BOOST_STATIC_ASSERT(CanFoo);
std::cout << "Foo" << std::endl;
}

};

int main(int argc, char** argv) {
A<false> cant_foo;
//cant_foo.foo();
A<true> can_foo;
can_foo.foo();
}
Remove the comment from cant_foo.foo() and you get a compile error.

john
Jul 22 '05 #2

Man, I hate acronyms! What's "SFINAE"?

(In the news business, most acronyms are spelled out the first time they're
used in an article. IMO [in my opinion], that would be a good idea in the
newsgroups as well. :-))

-Howard

Jul 22 '05 #3

"Howard" <al*****@hotmail.com> wrote in message
news:eV********************@bgtnsc04-news.ops.worldnet.att.net...

Man, I hate acronyms! What's "SFINAE"?
Substitution Failure Is Not An Error.
(In the news business, most acronyms are spelled out the first time they're used in an article. IMO [in my opinion], that would be a good idea in the
newsgroups as well. :-))


In this case, (IMO) if you don't know what the acronym stands for you
probably aren't going to be able to contribute. :-)

Jeff F
Jul 22 '05 #4
On Thu, 1 Jul 2004 14:01:25 -0400 in comp.lang.c++, "Jeff Flinn"
Substitution Failure Is Not An Error. In this case, (IMO) if you don't know what the acronym stands for you
probably aren't going to be able to contribute. :-)


But if the poster would spell it out, somebody else reading might learn
something.

Jul 22 '05 #5

"David Harmon" <so****@netcom.com.invalid> wrote in message
news:41***************@news.west.earthlink.net...
On Thu, 1 Jul 2004 14:01:25 -0400 in comp.lang.c++, "Jeff Flinn"
Substitution Failure Is Not An Error.

In this case, (IMO) if you don't know what the acronym stands for you
probably aren't going to be able to contribute. :-)


But if the poster would spell it out, somebody else reading might learn
something.


I googled SFINAE the first time I saw the acronym. I think that's why the
definition stuck with me. If I'd been spoon fed, who knows...

Jeff F
Jul 22 '05 #6
In article <opsagx7jcv212331@andronicus>, John Harrison wrote:
BOOST_STATIC_ASSERT(CanFoo);


Thank you for the hint about static assertions - this enabled me to
implement a solution to my problem.

--
Peter
Jul 22 '05 #7
On Fri, 2 Jul 2004 08:01:48 -0400 in comp.lang.c++, "Jeff Flinn"
<NO****@nowhere.com> wrote,
But if the poster would spell it out, somebody else reading might learn
something.


I googled SFINAE the first time I saw the acronym. I think that's why the
definition stuck with me. If I'd been spoon fed, who knows...


"Substitution Failure Is Not An Error" is sufficiently mysterious to
begin with.

Jul 22 '05 #8
David Harmon wrote:

On Fri, 2 Jul 2004 08:01:48 -0400 in comp.lang.c++, "Jeff Flinn"
<NO****@nowhere.com> wrote,
But if the poster would spell it out, somebody else reading might learn
something.


I googled SFINAE the first time I saw the acronym. I think that's why the
definition stuck with me. If I'd been spoon fed, who knows...


"Substitution Failure Is Not An Error" is sufficiently mysterious to
begin with.


Think of it as "now you see it, now you don't." Or, perhaps, NYSINYD.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #9

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

Similar topics

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...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
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: Kev | last post by:
Gidday, I am stuck trying to create a COM Callable Wrapper for the class (shell only) below. As you can see I have tried to define my interface and it would be all good if I wasn't passing my...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
1
by: siddhu | last post by:
Dear Experts, I am trying to implement "Is a class type" for template arguments using SFINAE. program is like this: #include<iostream> using namespace std; template <typename T>
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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.