How to check correctly for (const) overloaded method - c++

I am currently trying to get the following to compile:
class foo {
};
class bar {
public:
const foo & to_foo() const {
return f;
}
foo & to_foo() {
return f;
}
private:
foo f;
};
template< typename T, typename Enable = void >
class convert {};
template< typename T >
struct convert< T, typename std::enable_if< std::is_member_function_pointer< decltype( &T::to_foo ) >::value >::type > {
static const foo & call1( const bar & b ) {
return b.to_foo();
}
static foo & call2( bar & b ) {
return b.to_foo();
}
};
However the specialisation get's confused by the presence of two possible to_foo() members, so it will choose the default case. As soon as I remove one of the to_foo() members, it works, but then one of the callX() methods fails because it does not match the constness.
Is there any way to detect this function in this case?
EDIT:
Here is an example on ideone: http://ideone.com/E6saX
When one of the the methods is removed, it works just fine: http://ideone.com/iBKoN

It is still a bit unclear to me what you are trying to achieve. I will suppose that the target type (foo) is fixed and we are not attempting to create a full bridge system.
In this case, we can ditch the structure and just rely on overload selection.
foo const& to_foo(bar const& b) { return b.to_foo(); }
foo& to_foo(bar& b) { return b.to_foo(); }
Works just fine, as far as the actual translation goes. No template involved.
Now the question might be how to actually detect whether this conversion is possible or not. In this case, we need to use SFINAE to avoid a hard-error while attempting the conversion.
#include <iostream>
#include <utility>
// Didn't remember where this is implemented, oh well
template <typename T, typename U> struct same_type: std::false_type {};
template <typename T> struct same_type<T, T>: std::true_type {};
// Types to play with
struct Foo {};
struct Bar { Foo _foo; };
struct Bad {};
Foo const& to_foo(Bar const& b) { return b._foo; }
Foo& to_foo(Bar& b) { return b._foo; }
// Checker
template <typename T>
struct ToFoo {
T const& _crt;
T& _rt;
template <typename U>
static auto to_foo_exists(U const& crt, U& rt) ->
decltype(to_foo(crt), to_foo(rt), std::true_type());
static std::false_type to_foo_exists(...);
// Work around as the following does not seem to work
// static bool const value = decltype(to_foo_exists(_crt, _rt))::value;
static bool const value = same_type<
decltype(to_foo_exists(_crt, _rt)),
std::true_type
>::value;
};
// Proof
int main() {
std::cout << ToFoo<Bar>::value << "\n"; // true
std::cout << ToFoo<Bad>::value << "\n"; // false
}
Note: successfully compiled on Clang 3.0 (with the work around) and gcc 4.5.1.

I don't know much about templates yet, but it seems that is_const is the type trait you're looking for, to check as to whether the function is const.
Link here

my gcc(4.1.0) is not support c++0x, so i remove the std::enable_if part. then it compiles and runs successfullly.
see:
http://ideone.com/KzasX
thanks

Related

How do you specialize a member function inside a template class?

Let's say I have the following class:
template <typename T>
class SomeClass : Parent<T>
{
public:
// I have a function such as this one:
T DoSomething(const T &t)
{
return t.DoSomething(some_data);
}
// But `T` might be a pointer, so sometimes I will need something like the following
// instead (which obviously doesn't work as given):
T DoSomething(const T &t)
{
return new T(t->DoSomething(some_data));
}
private:
XYZ some_data;
};
I got stuck in a giant mess of template errors trying to implement this in any semi-nice way possible using template specialization.
In the end I came up with this very ugly solution:
template <typename T>
class SomeClass : Parent<T>
{
public:
T DoSomething(const T &x)
{
return Specializer<T>::Do(this, x);
}
private:
template <typename V>
struct Specializer {
static V Do(SomeClass *me, const V &x)
{
return x.DoSomething(me->some_data);
}
};
template <typename V>
struct Specializer<V*> {
static V* Do(SomeClass *me, const V *&x)
{
return new V(x->DoSomething(me->some_data));
}
};
XYZ some_data;
};
Is there a better way to do this that doesn't involve stuffing this function into a dummy class/struct and passing around my this pointer?
PS: In reality, this has nothing to do with pointers, but rather with different types of containers. Pointers were just an easy example to use here.
You can avoid writing any specializations, and use a type trait like std::is_pointer along with if constexpr to decide what code to execute depending on the whether the type is a pointer type or not:
auto DoSomething(const T &t)
{
if constexpr (std::is_pointer_v<T>)
return new T(t->DoSomething(some_data));
else
return t.DoSomething(some_data);
}
If you don't want to check for whether T is a pointer, but want to check something else, you can still use this pattern by dropping in a suitable replacement for is_pointer.
If you have access to c++20, you can clean up the need for any SFINAE, specializations, or if constexpr by using concepts and constraints instead. This just allows you to define the same function N times with different criteria for its insantiation, which is much more readable IMO.
This is almost the same as the SFINAE approach, but without the need for the awful syntax (no std::declval, decltype, etc). It also doesn't require all implementations to exist in one function definition like the if constexpr approach; all you need is separate function definitions with different requires clauses:
#include <concepts>
...
template <typename T>
class SomeClass : Parent<T>
{
public:
// Work for everything that's not specialized
void DoSomething(const T &t)
{
std::cout << "Basic overload" << std::endl;
}
// Only work for pointers
void DoSomething(const T& t) requires(std::is_pointer_v<T>)
{
std::cout << "Pointer overload" << std::endl;
}
// Only work if T is convertible to SomeType
void DoSomething(const T& t) requires(std::convertible_to<T, SomeType>)
{
std::cout << "Convertible to SomeType overload" << std::endl;
}
private:
XYZ some_data;
};
Live Example
In this approach there are 3 different entries:
The basic fallback for all templates
An implementation that works for any pointer type, and
An implementation that works for any T type that may be convertible to SomeType
What about using SFINAE?
For example
#include <utility>
#include <iostream>
template <typename>
struct Parent
{ };
using XYZ = int;
template <typename T>
class SomeClass : Parent<T>
{
public:
template <typename U = T>
auto DoSomething (T const & t)
-> decltype( std::declval<U>().DoSomething(std::declval<XYZ>()) )
{ std::cout << "ref\n"; return t.DoSomething(some_data); }
template <typename U = T>
auto DoSomething (T const & t)
-> std::remove_reference_t<
decltype( std::declval<U>()->DoSomething(std::declval<XYZ>()),
std::declval<T>() )>
{
using V = std::remove_reference_t<decltype(*t)>;
std::cout << "pnt\n"; return new V(t->DoSomething(some_data));
}
private:
XYZ some_data;
};
struct foo
{
foo (foo*) {}
foo () {}
foo DoSomething (int) const { return {}; }
} ;
int main()
{
SomeClass<foo> sc1;
SomeClass<foo*> sc2;
foo f;
sc1.DoSomething(f);
sc2.DoSomething(&f);
}
I mean: what about enabling the first version if, and only if, T is a type that supports a DoSomething(XYZ) method and enabling the second version if, and only if, T is a pointer of a type that supports a DoSomething(XYZ) method?

c++ Templates to change constness of a function

I am interested in designing a template interface where the const ness of the function and return type itself changes depending on the template parameter. I have managed to do this for the return type as follows.
template<typename T, bool canChange>
struct Changable{};
template<typename T>
struct Changable<T,true>
{
typedef T type;
};
template<typename T>
struct Changable<T,false>
{
typedef const T type;
};
template<typename T, bool canChange>
struct Data{
typedef typename Changable<T,canChange>::type DataType;
DataType m_data; //< This makes it const/non-const at compile time.
// This function will also make the return type const/non-const
// at compile time.
DataType& GetDataRef(){ return m_data;}
//However, it seems to me that I still need a second function
//with an explicit "const", which I can't seem to avoid.
DataType& GetDataRef()const{return m_data;}
};
Can I somehow avoid having two const/non-const functions here at compile time using some SFINAE magic? std::enable_if would have been ideal here but it seems to me that const is not a type and that approach may not work. Any suggestions?
Here is an example based on inheritance:
#include <type_traits>
#include <iostream>
template<typename T, bool canChange>
struct Changable { using type = const T; };
template<typename T>
struct Changable<T, true> { using type = std::decay_t<T>; };
template<typename, typename, bool>
struct Base;
template<typename D, typename T>
struct Base<D, T, true> {
using DataType = typename Changable<T, true>::type;
DataType& GetDataRef() { std::cout << "non-const" << std::endl; return static_cast<D*>(this)->m_data; }
};
template<typename D, typename T>
struct Base<D, T, false> {
using DataType = typename Changable<T, false>::type;
DataType& GetDataRef() const { std::cout << "const" << std::endl; return static_cast<const D*>(this)->m_data; }
};
template<typename T, bool canChange>
struct Data: Base<Data<T, canChange>, T, canChange> {
friend class Base<Data<T, canChange>, T, canChange>;
typename Base<Data<T, canChange>, T, canChange>::DataType m_data{};
using Base<Data<T, canChange>, T, canChange>::GetDataRef;
};
int main() {
Data<int, true> d1;
Data<int, false> d2;
d1.GetDataRef();
d2.GetDataRef();
}
As requested, Data has only one definition of the GetDataRef method.
Which one is available, the const one or the other one, depends on the value of canChange.
Note the friend declaration. It allows the base class to access to the private data members of Data.
I think I'd approach this using the templates already available in the standard library. It does not require inheritance or any custom classes.
#include <utility>
template<typename T, bool canChange>
struct Data{
using value_type = T;
using cv_type = std::conditional_t<canChange, value_type, std::add_const_t<value_type>>;
using reference = std::add_lvalue_reference_t<cv_type>;
using const_reference = std::add_lvalue_reference_t<std::add_const_t<cv_type>>;
Data(T t) : m_data(std::move(t)) {}
cv_type m_data; //< This makes it const/non-const at compile time.
// This function will also make the return type const/non-const
// at compile time.
reference GetDataRef(){ return m_data;}
//However, it seems to me that I still need a second function
//with an explicit "const", which I can't seem to avoid.
const_reference GetDataRef() const {return m_data;}
};
int main()
{
Data<int, true> d1 { 10 };
d1.m_data = 12;
const Data<int, true>& rd1 = d1;
auto& a = d1.GetDataRef();
auto& b = rd1.GetDataRef();
a = 12; // compiles fine
// b= 12; won't compile
Data<int, false> d2 { 10 };
const Data<int, false>& rd2 = d2;
auto& c = d2.GetDataRef();
auto& d = rd2.GetDataRef();
// c = 12; // won't compile
// d = 12; // won't compile
}
Now to the question:
Can I somehow avoid having two const/non-const functions here at compile time using some SFINAE magic?
You're almost answering your own question here. SFINAE requires that template arguments are considered in immediate context. Which is a complex way of saying that the expression in std::enable_if<> must depend on some template type.
Unhappily, the template type of T is known by the time the function GetDataRef is evaluated, so enable_if won't help us here.
So if we only want one version of GetDataRef we would indeed have to resort to derivation from a template type (the base class would then be evaluated in immediate context of T).
However, there is a problem even then.
consider:
Data<int, true>& x This is a reference to mutable container containing mutable data
const Data<int, true>& y This is a reference to an immutable container containing mutable data
calling x.GetDataRef() ought to return a mutable reference to an int, otherwise we'll confuse our users.
calling y.GetDataRef() should certainly return a const reference to an int, otherwise again, users may be shocked to learn that a member of a const thing is actually mutable.
Maybe something like this can solve the issue:
#include <type_traits>
#include <iostream>
template<typename T, bool canChange>
struct Changable: std::false_type { using type = const T; };
template<typename T>
struct Changable<T, true>: std::true_type { using type = std::decay_t<T>; };
template<typename T, bool canChange>
struct Data {
using DataTraits = Changable<T, canChange>;
private:
template<typename U>
std::enable_if_t<U::value, typename U::type&>
GetDataRefImpl() { std::cout << "non const" << std::endl; return m_data; }
template<typename U>
std::enable_if_t<not U::value, typename U::type&>
GetDataRefImpl() const { std::cout << "const" << std::endl; return m_data; }
public:
typename DataTraits::type m_data{};
typename DataTraits::type& GetDataRef() { return GetDataRefImpl<DataTraits>(); }
typename DataTraits::type& GetDataRef() const { return GetDataRefImpl<DataTraits>(); }
};
int main() {
Data<int, true> d1;
Data<int, false> d2;
d1.GetDataRef();
d2.GetDataRef();
}
The basic idea is to have both the functions exposed by the class, then forward them internally to the same sfinaed one that is const or non-const (this depends on the value of canChange).
As you can see by running the example, the result is:
non const
const
This is true even if both d1 and d2 have been defined as non const.
The std::enable_if turn on the right internal function at compile time.
Note that I've used what the C++14 offers (as an example std::enable_if_t).
The example can be easily converted to a C++11 based on (std::enable_if_t is nothing more than typename std::enable_if<condition, type>::type and so on).
If C++17 is available to you, have a look at is_const, add_const and remove_const.
Together with if constexpr (), a rather elegant solution should be possible.

Determining inheritance relationships among type-erased classes

I have a typical type-erasure setup:
struct TEBase
{
virtual ~TEBase() {}
// ...
};
template <typename T>
struct TEImpl : TEBase
{
// ...
};
Now the question: Given a second class hierarchy like this,
struct Foo { };
struct Bar : Foo { };
struct Unrelated { };
is it possible, given a TEBase * p, to determine whether the dynamic type of *p is of the form TEImpl<X>, where, X derives from Foo? In other words, I want function:
template <typename T> bool is_derived_from(TEBase * p);
such that:
is_derived_from<Foo>(new TEImpl<Foo>) == true
is_derived_from<Foo>(new TEImpl<Bar>) == true
is_derived_from<Foo>(new TEImpl<Unrelated>) == false
In particular, I'm looking for a solution that is general, non-intrusive, and efficient. I've found two solutions to this problem (posted below as answers) but neither of them solve all three criteria.
Something like this:
template <typename Type, typename UnaryPredicate>
void DoPred(UnaryPredicate pred)
{
if (T * p = dynamic_cast<Derived<T> *>(this))
{
return pred(p->type);
}
return false;
}
This isn't 100% universal, since you cannot, for example, say DoPred<int>. A more universal solution would add a virtual std::type_info type() const { return typeid(...); } member function to the hierarchy and use that to determine if the type matches (the standard type erasure idiom). Both approaches use the same sort of RTTI, though.
After the clarification:
Right now, I don't think this can be solved. All you have is a TEBase subobject. It could be part of a TEImpl<Bar>, or part of a TEImpl<Unrelated>, but neither of those types is related to TEImpl<Foo>, which is what you're after.
You're essentially asking that TEImpl<Bar> derives from TEImpl<Foo>. To do this, you would actually want TEImpl<T> to inherit from all TEImpl<std::direct_bases<T>::type>..., if you see what I mean. This is not possible in C++11, but will be possible in TR2. GCC already supports it. Here is an example implementation. (It causes a warning due to ambiguous bases, which could be avoided with more work, but it works nonetheless.)
#include <tr2/type_traits>
struct TEBase { virtual ~TEBase() {} };
template <typename T> struct TEImpl;
template <typename TL> struct Derivator;
template <typename TL, bool EmptyTL>
struct DerivatorImpl;
template <typename TL>
struct DerivatorImpl<TL, true>
: TEBase
{ };
template <typename TL>
struct DerivatorImpl<TL, false>
: TEImpl<typename TL::first::type>
, Derivator<typename TL::rest::type>
{ };
template <typename TL>
struct Derivator
: DerivatorImpl<TL, TL::empty::value>
{ };
template <typename T>
struct TEImpl
: Derivator<typename std::tr2::direct_bases<T>::type>
{
};
template <typename T>
bool is(TEBase const * b)
{
return nullptr != dynamic_cast<TEImpl<T> const *>(b);
}
struct Foo {};
struct Bar : Foo {};
struct Unrelated {};
#include <iostream>
#include <iomanip>
int main()
{
TEImpl<int> x;
TEImpl<Unrelated> y;
TEImpl<Bar> z;
TEImpl<Foo> c;
std::cout << std::boolalpha << "int ?< Foo: " << is<Foo>(&x) << "\n";
std::cout << std::boolalpha << "Unr ?< Foo: " << is<Foo>(&y) << "\n";
std::cout << std::boolalpha << "Bar ?< Foo: " << is<Foo>(&z) << "\n";
std::cout << std::boolalpha << "Foo ?< Foo: " << is<Foo>(&c) << "\n";
}
I would suggest reading the article Generic Programming:Typelists and Applications. There Andrei Alexandrescu desribes an implementation of a ad-hoc Visitor which should solve your problem. Another good resource would be his book Moder C++ Design where he describes a multidispatcher in a Brute Force way which uses the same approuch (pages 265 ...).
In my opinion these 2 resources are better for understanding than any code which could be printed here.
This solution involves abusing exceptions a bit. If the TEImpl type simply throws its data, is_derived_from can catch the type it's looking for.
struct TEBase
{
virtual ~TEBase() {}
virtual void throw_data() = 0;
};
template <typename T>
struct TEImpl : public TEBase
{
void throw_data() {
throw &data;
}
T data;
};
template <typename T>
bool is_derived_from(TEBase* p)
{
try {
p->throw_data();
} catch (T*) {
return true;
} catch (...) {
// Do nothing
}
return false;
}
This solution works great. It works perfectly with any inheritance structure, and it's completely non-intrusive.
The only problem is that it's no efficient at all. Exceptions were not intended to be used in this way, and I suspect this solution is thousands of times slower than other solutions.
This solution involves comparing typeids. TEImpl knows its own type, so it can check a passed typeid against its own.
The trouble is, this technique doesn't work when you add inheritance, so I'm also using template meta-programming to check if the type has typedef super defined, in which case it will recursively check its parent class.
struct TEBase
{
virtual ~TEBase() {}
virtual bool is_type(const type_info& ti) = 0;
};
template <typename T>
struct TEImpl : public TEBase
{
bool is_type(const type_info& ti) {
return is_type_impl<T>(ti);
}
template <typename Haystack>
static bool is_type_impl(const type_info& ti) {
return is_type_super<Haystack>(ti, nullptr);
}
template <typename Haystack>
static bool is_type_super(const type_info& ti, typename Haystack::super*) {
if(typeid( Haystack ) == ti) return true;
return is_type_impl<typename Haystack::super>(ti);
}
template <typename Haystack>
static bool is_type_super(const type_info& ti, ...) {
return typeid(Haystack) == ti;
}
};
template <typename T>
bool is_derived_from(TEBase* p)
{
return p->is_type(typeid( T ));
}
For this to work with, Bar needs to be redefined as:
struct Bar : public Foo
{
typedef Foo super;
};
This should be fairly efficient, but it's obviously not non-intrusive, since it requires a typedef super in the target class whenever inheritance is being used. The typedef super also has to be publicly accessible, which goes against what many consider to be a recommended practice of putting your typedef super in your private section.
It also doesn't deal with multiple-inheritance at all.
Update: This solution can be taken further to make it general and non-intrusive.
Making it general
typedef super is great, because it's idiomatic and already used in many classes, but it doesn't allow multiple inheritance. In order to do that, we'll need to replace it with a type that can store multiple types, such as a tuple.
If Bar was rewritten as:
struct Bar : public Foo, public Baz
{
typedef tuple<Foo, Baz> supers;
};
we could support this form of declaration by adding the following code to TEImpl:
template <typename Haystack>
static bool is_type_impl(const type_info& ti) {
// Redefined to call is_type_supers instead of is_type_super
return is_type_supers<Haystack>(ti, nullptr);
}
template <typename Haystack>
static bool is_type_supers(const type_info& ti, typename Haystack::supers*) {
return IsTypeTuple<typename Haystack::supers, tuple_size<typename Haystack::supers>::value>::match(ti);
}
template <typename Haystack>
static bool is_type_supers(const type_info& ti, ...) {
return is_type_super<Haystack>(ti, nullptr);
}
template <typename Haystack, size_t N>
struct IsTypeTuple
{
static bool match(const type_info& ti) {
if(is_type_impl<typename tuple_element< N-1, Haystack >::type>( ti )) return true;
return IsTypeTuple<Haystack, N-1>::match(ti);
}
};
template <typename Haystack>
struct IsTypeTuple<Haystack, 0>
{
static bool match(const type_info& ti) { return false; }
};
Making it non-intrusive
Now we have a solution which is efficient and general, but it's still intrusive, so it won't support classes that can't be modified.
To support this, we'll need a way to declare the object inheritance from outside the class. For Foo, we could do something like this:
template <>
struct ClassHierarchy<Bar>
{
typedef tuple<Foo, Baz> supers;
};
To support that style, first we need the non-specialized form of ClassHierarchy, which we'll define like so:
template <typename T> struct ClassHierarchy { typedef bool undefined; };
We'll use the presence of undefined to tell whether or not the class has been specialized.
Now we need to add some more functions to TEImpl. We'll still reuse most of the code from earlier, but now we'll also support reading the type data from ClassHierarchy.
template <typename Haystack>
static bool is_type_impl(const type_info& ti) {
// Redefined to call is_type_external instead of is_type_supers.
return is_type_external<Haystack>(ti, nullptr);
}
template <typename Haystack>
static bool is_type_external(const type_info& ti, typename ClassHierarchy<Haystack>::undefined*) {
return is_type_supers<Haystack>(ti, nullptr);
}
template <typename Haystack>
static bool is_type_external(const type_info& ti, ...) {
return is_type_supers<ClassHierarchy< Haystack >>(ti, nullptr);
}
template <typename Haystack>
struct ActualType
{
typedef Haystack type;
};
template <typename Haystack>
struct ActualType<ClassHierarchy< Haystack >>
{
typedef Haystack type;
};
template <typename Haystack>
static bool is_type_super(const type_info& ti, ...) {
// Redefined to reference ActualType
return typeid(typename ActualType<Haystack>::type) == ti;
}
And now we have a solution which is efficient, general, and non-intrusive.
Future solution
This solution meets the criteria, but it's still a little annoying to have to document the class hierarchy explicitly. The compiler already knows everything about the class hierarchy, so it's a shame that we have to do this grunt work.
A proposed solution to this problem is N2965: Type traits and base classes, which has been implemented in GCC. This paper defines a direct_bases class, which is almost identical to our ClassHierarchy class, except its only element, type, is guaranteed to be a tuple, like supers, and the class is completely generated by the compiler.
So for now we have to write a little boilerplate to get this to work, but if N2965 gets accepted, we can get rid of the boilerplate and make TEImpl much shorter.
Special thanks to Kerrek SB and Jan Herrmann. This answer drew a lot of inspiration from their comments.

Detecting a function in C++ at compile time

Is there a way, presumably using templates, macros or a combination of the two, that I can generically apply a function to different classes of objects but have them respond in different ways if they do not have a specific function?
I specifically want to apply a function which will output the size of the object (i.e. the number of objects in a collection) if the object has that function but will output a simple replacement (such as "N/A") if the object doesn't. I.e.
NO_OF_ELEMENTS( mySTLMap ) -----> [ calls mySTLMap.size() to give ] ------> 10
NO_OF_ELEMENTS( myNoSizeObj ) --> [ applies compile time logic to give ] -> "N/A"
I expect that this might be something similar to a static assertion although I'd clearly want to compile a different code path rather than fail at build stage.
From what I understand, you want to have a generic test to see if a class has a certain member function. This can be accomplished in C++ using SFINAE. In C++11 it's pretty simple, since you can use decltype:
template <typename T>
struct has_size {
private:
template <typename U>
static decltype(std::declval<U>().size(), void(), std::true_type()) test(int);
template <typename>
static std::false_type test(...);
public:
typedef decltype(test<T>(0)) type;
enum { value = type::value };
};
If you use C++03 it is a bit harder due to the lack of decltype, so you have to abuse sizeof instead:
template <typename T>
struct has_size {
private:
struct yes { int x; };
struct no {yes x[4]; };
template <typename U>
static typename boost::enable_if_c<sizeof(static_cast<U*>(0)->size(), void(), int()) == sizeof(int), yes>::type test(int);
template <typename>
static no test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(yes) };
};
Of course this uses Boost.Enable_If, which might be an unwanted (and unnecessary) dependency. However writing enable_if yourself is dead simple:
template<bool Cond, typename T> enable_if;
template<typename T> enable_if<true, T> { typedef T type; };
In both cases the method signature test<U>(int) is only visible, if U has a size method, since otherwise evaluating either the decltype or the sizeof (depending on which version you use) will fail, which will then remove the method from consideration (due to SFINAE. The lengthy expressions std::declval<U>().size(), void(), std::true_type() is an abuse of C++ comma operator, which will return the last expression from the comma-separated list, so this makes sure the type is known as std::true_type for the C++11 variant (and the sizeof evaluates int for the C++03 variant). The void() in the middle is only there to make sure there are no strange overloads of the comma operator interfering with the evaluation.
Of course this will return true if T has a size method which is callable without arguments, but gives no guarantees about the return value. I assume wou probably want to detect only those methods which don't return void. This can be easily accomplished with a slight modification of the test(int) method:
// C++11
template <typename U>
static typename std::enable_if<!is_void<decltype(std::declval<U>().size())>::value, std::true_type>::type test(int);
//C++03
template <typename U>
static typename std::enable_if<boost::enable_if_c<sizeof(static_cast<U*>(0)->size()) != sizeof(void()), yes>::type test(int);
There was a discussion about the abilities of constexpr some times ago. It's time to use it I think :)
It is easy to design a trait with constexpr and decltype:
template <typename T>
constexpr decltype(std::declval<T>().size(), true) has_size(int) { return true; }
template <typename T>
constexpr bool has_size(...) { return false; }
So easy in fact that the trait loses most of its value:
#include <iostream>
#include <vector>
template <typename T>
auto print_size(T const& t) -> decltype(t.size(), void()) {
std::cout << t.size() << "\n";
}
void print_size(...) { std::cout << "N/A\n"; }
int main() {
print_size(std::vector<int>{1, 2, 3});
print_size(1);
}
In action:
3
N/A
This can be done using a technique called SFINAE. In your specific case you could implement that using Boost.Concept Check. You'd have to write your own concept for checking for a size-method. Alternatively you could use an existing concept such as Container, which, among others, requires a size-method.
You can do something like
template< typename T>
int getSize(const T& t)
{
return -1;
}
template< typename T>
int getSize( const std::vector<T>& t)
{
return t.size();
}
template< typename T , typename U>
int getSize( const std::map<T,U>& t)
{
return t.size();
}
//Implement this interface for
//other objects
class ISupportsGetSize
{
public:
virtual int size() const= 0;
};
int getSize( const ISupportsGetSize & t )
{
return t.size();
}
int main()
{
int s = getSize( 4 );
std::vector<int> v;
s = getSize( v );
return 0;
}
basically the most generic implementation is always return -1 or "NA" but for vector and maps it will return the size. As the most general one always matches there is never a build time failure
Here you go. Replace std::cout with the output of your liking.
template <typename T>
class has_size
{
template <typename C> static char test( typeof(&C::size) ) ;
template <typename C> static long test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
template<bool T>
struct outputter
{
template< typename C >
static void output( const C& object )
{
std::cout << object.size();
}
};
template<>
struct outputter<false>
{
template< typename C >
static void output( const C& )
{
std::cout << "N/A";
}
};
template<typename T>
void NO_OF_ELEMENTS( const T &object )
{
outputter< has_size<T>::value >::output( object );
}
You could try something like:
#include <iostream>
#include <vector>
template<typename T>
struct has_size
{
typedef char one;
typedef struct { char a[2]; } two;
template<typename Sig>
struct select
{
};
template<typename U>
static one check (U*, select<char (&)[((&U::size)!=0)]>* const = 0);
static two check (...);
static bool const value = sizeof (one) == sizeof (check (static_cast<T*> (0)));
};
struct A{ };
int main ( )
{
std::cout << has_size<int>::value << "\n";
std::cout << has_size<A>::value << "\n";
std::cout << has_size<std::vector<int>>::value << "\n";
}
but you have to be careful, this does neither work when size is overloaded, nor when it is a template. When you can use C++11, you can replace the above sizeof trick by decltype magic

c++ template specialization for all subclasses

I need to create a template function like this:
template<typename T>
void foo(T a)
{
if (T is a subclass of class Bar)
do this
else
do something else
}
I can also imagine doing it using template specialization ... but I have never seen a template specialization for all subclasses of a superclass. I don't want to repeat specialization code for each subclass
You can do what you want but not how you are trying to do it! You can use std::enable_if together with std::is_base_of:
#include <iostream>
#include <utility>
#include <type_traits>
struct Bar { virtual ~Bar() {} };
struct Foo: Bar {};
struct Faz {};
template <typename T>
typename std::enable_if<std::is_base_of<Bar, T>::value>::type
foo(char const* type, T) {
std::cout << type << " is derived from Bar\n";
}
template <typename T>
typename std::enable_if<!std::is_base_of<Bar, T>::value>::type
foo(char const* type, T) {
std::cout << type << " is NOT derived from Bar\n";
}
int main()
{
foo("Foo", Foo());
foo("Faz", Faz());
}
Since this stuff gets more wide-spread, people have discussed having some sort of static if but so far it hasn't come into existance.
Both std::enable_if and std::is_base_of (declared in <type_traits>) are new in C++2011. If you need to compile with a C++2003 compiler you can either use their implementation from Boost (you need to change the namespace to boost and include "boost/utility.hpp" and "boost/enable_if.hpp" instead of the respective standard headers). Alternatively, if you can't use Boost, both of these class template can be implemented quite easily.
I would use std::is_base_of along with local class as :
#include <type_traits> //you must include this: C++11 solution!
template<typename T>
void foo(T a)
{
struct local
{
static void do_work(T & a, std::true_type const &)
{
//T is derived from Bar
}
static void do_work(T & a, std::false_type const &)
{
//T is not derived from Bar
}
};
local::do_work(a, std::is_base_of<Bar,T>());
}
Please note that std::is_base_of derives from std::integral_constant, so an object of former type can implicitly be converted into an object of latter type, which means std::is_base_of<Bar,T>() will convert into std::true_type or std::false_type depending upon the value of T. Also note that std::true_type and std::false_type are nothing but just typedefs, defined as:
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
I know this question has been answered but nobody mentioned that std::enable_if can be used as a second template parameter like this:
#include <type_traits>
class A {};
class B: public A {};
template<class T, typename std::enable_if<std::is_base_of<A, T>::value, int>::type = 0>
int foo(T t)
{
return 1;
}
I like this clear style:
void foo_detail(T a, const std::true_type&)
{
//do sub-class thing
}
void foo_detail(T a, const std::false_type&)
{
//do else
}
void foo(T a)
{
foo_detail(a, std::is_base_of<Bar, T>::value);
}
The problem is that indeed you cannot do something like this in C++17:
template<T>
struct convert_t {
static auto convert(T t) { /* err: no specialization */ }
}
template<T>
struct convert_t<T> {
// T should be subject to the constraint that it's a subclass of X
}
There are, however, two options to have the compiler select the correct method based on the class hierarchy involving tag dispatching and SFINAE.
Let's start with tag dispatching. The key here is that tag chosen is a pointer type. If B inherits from A, an overload with A* is selected for a value of type B*:
#include <iostream>
#include <type_traits>
struct type_to_convert {
type_to_convert(int i) : i(i) {};
type_to_convert(const type_to_convert&) = delete;
type_to_convert(type_to_convert&&) = delete;
int i;
};
struct X {
X(int i) : i(i) {};
X(const X &) = delete;
X(X &&) = delete;
public:
int i;
};
struct Y : X {
Y(int i) : X{i + 1} {}
};
struct A {};
template<typename>
static auto convert(const type_to_convert &t, int *) {
return t.i;
}
template<typename U>
static auto convert(const type_to_convert &t, X *) {
return U{t.i}; // will instantiate either X or a subtype
}
template<typename>
static auto convert(const type_to_convert &t, A *) {
return 42;
}
template<typename T /* requested type, though not necessarily gotten */>
static auto convert(const type_to_convert &t) {
return convert<T>(t, static_cast<T*>(nullptr));
}
int main() {
std::cout << convert<int>(type_to_convert{5}) << std::endl;
std::cout << convert<X>(type_to_convert{6}).i << std::endl;
std::cout << convert<Y>(type_to_convert{6}).i << std::endl;
std::cout << convert<A>(type_to_convert{-1}) << std::endl;
return 0;
}
Another option is to use SFINAE with enable_if. The key here is that while the snippet in the beginning of the question is invalid, this specialization isn't:
template<T, typename = void>
struct convert_t {
static auto convert(T t) { /* err: no specialization */ }
}
template<T>
struct convert_t<T, void> {
}
So our specializations can keep a fully generic first parameter as long we make sure only one of them is valid at any given point. For this, we need to fashion mutually exclusive conditions. Example:
template<typename T /* requested type, though not necessarily gotten */,
typename = void>
struct convert_t {
static auto convert(const type_to_convert &t) {
static_assert(!sizeof(T), "no conversion");
}
};
template<>
struct convert_t<int> {
static auto convert(const type_to_convert &t) {
return t.i;
}
};
template<typename T>
struct convert_t<T, std::enable_if_t<std::is_base_of_v<X, T>>> {
static auto convert(const type_to_convert &t) {
return T{t.i}; // will instantiate either X or a subtype
}
};
template<typename T>
struct convert_t<T, std::enable_if_t<std::is_base_of_v<A, T>>> {
static auto convert(const type_to_convert &t) {
return 42; // will instantiate either X or a subtype
}
};
template<typename T>
auto convert(const type_to_convert& t) {
return convert_t<T>::convert(t);
}
Note: the specific example in the text of the question can be solved with constexpr, though:
template<typename T>
void foo(T a) {
if constexpr(std::is_base_of_v<Bar, T>)
// do this
else
// do something else
}
If you are allowed to use C++20 concepts, all this becomes almost trivial:
template<typename T> concept IsChildOfX = std::is_base_of<X, T>::value;
// then...
template<IsChildOfX X>
void somefunc( X& x ) {...}