SFINAE

K

kaalus

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
 
R

Rolf Magnus

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.
 
V

Victor Bazarov

Rolf said:
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
 
P

peter koch

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
 
N

n2xssvv g02gfr12930

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::eek:stream &operator<<(std::eek:stream &str, const MyOutput
&inp);
};

template <typename T>
std::eek:stream &operator<<(std::eek: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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top