Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

SFINAE

 
 
kaalus@gmail.com
Guest
Posts: n/a
 
      04-26-2006
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 output1(const T &t)
{
// This implementation should be in effect for types that support
<<
std::cout << t;
}

template<class T>
void output2(const T &t)
{
// This implementation should be in effect for types that do not
support <<
my_output(t);
}

So that this usage is possible:

T t;
output(t); // uses output1 or output2 depending on << operator
defined for T

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      04-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Is it possible to use SFINAE to provide different implementations of
> some function depending on the fact that operator << is overloaded for
> some type?


I have no idea what a "SFINAE" is.

> For example:
>
> template<class T>
> void output1(const T &t)
> {
> // This implementation should be in effect for types that support
> <<
> std::cout << t;
> }
>
> template<class T>
> void output2(const T &t)
> {
> // This implementation should be in effect for types that do not
> support <<
> my_output(t);
> }
>
> So that this usage is possible:
>
> T t;
> output(t); // uses output1 or output2 depending on << operator
> defined for T


You could use some kind of traits class.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-26-2006
Rolf Magnus wrote:
> (E-Mail Removed) wrote:
>
>> Is it possible to use SFINAE to provide different implementations of
>> some function depending on the fact that operator << is overloaded
>> for some type?

>
> I have no idea what a "SFINAE" is.


REALLY?!! OK, I'll be the first to tell you. It stands for "Substitution
Failure Is Not An Error". Look it up on the Web. It's a technique you can
use to play some interesting tricks using templates.

> [...]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      04-26-2006

(E-Mail Removed) wrote:
> Is it possible to use SFINAE to provide different implementations of
> some function depending on the fact that operator << is overloaded for
> some type?


It should be: what is your problem?
>
> For example:
>
> template<class T>
> void output1(const T &t)
> {
> // This implementation should be in effect for types that support
> <<
> std::cout << t;
> }
>
> template<class T>
> void output2(const T &t)
> {
> // This implementation should be in effect for types that do not
> support <<
> my_output(t);
> }
>
> So that this usage is possible:
>
> T t;
> output(t); // uses output1 or output2 depending on << operator
> defined for T


I do not really see the reason to do it with SFINAE (Substitution
Failure Is Not An Error). The most obvious way to do solve your problem
would be to provide operator<< for those classes. If that is not
appropriate, I believe I would simply provide:

template <typename T>
void my_output(T const& t) { std::cout << t; }

/Peter

 
Reply With Quote
 
n2xssvv g02gfr12930
Guest
Posts: n/a
 
      04-26-2006
(E-Mail Removed) wrote:
> 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 output1(const T &t)
> {
> // This implementation should be in effect for types that support
> <<
> std::cout << t;
> }
>
> template<class T>
> void output2(const T &t)
> {
> // This implementation should be in effect for types that do not
> support <<
> my_output(t);
> }
>
> So that this usage is possible:
>
> T t;
> output(t); // uses output1 or output2 depending on << operator
> defined for T
>


Why not create a container class template something like as follows:

template <typename T>
class My_Output
{
private:
const T &val:
public:
My_Output(const T &val) : val(inp)
{
}
friend std:stream &operator<<(std:stream &str, const MyOutput
&inp);
};

template <typename T>
std:stream &operator<<(std:stream &str, const MyOutput<T> &inp)
{
// Add your code here

return str;
}

MyClass special;
// Your code here
std::cout << My_Output(special) << std::endl;

Hope this helps.

JB
 
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 problem. christopher diggins C++ 4 09-26-2005 06:25 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