Determine if type supports operation at compile time

  • Thread starter Marcin Kalicinski
  • Start date
M

Marcin Kalicinski

Is there a way, using template metaprogramming techinques like e.g. SFINAE,
to determine at compile time if a type supports certain operation (for
example if operator == is defined for that type)?

// Function for types supporting ==
template<class T> bool func(T t)
{
if (t == 3)
return true;
else
return false;
}

// Function for types not supporting ==
template<class T> bool func(T t)
{
return false;
}

Can I somehow make compiler call appropriate function depending on type of
T?

thanks,
Marcin
 
K

kwikius

Is there a way, using template metaprogramming techinques like e.g. SFINAE,
to determine at compile time if a type supports certain operation (for
example if operator == is defined for that type)?

// Function for types supporting ==
template<class T> bool func(T t)
{
if (t == 3)
return true;
else
return false;

}

// Function for types not supporting ==
template<class T> bool func(T t)
{
return false;

}

Can I somehow make compiler call appropriate function depending on type of
T?

thanks,
Marcin

Using the example of ==, there are plans to incorporate this type of
functionality into the next version of C++.

See the ConceptG++ EqualityComparable concept:

http://tinyurl.com/32q2bq

regards
Andy Little
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top