typetraits

isAbstract template and visual studio

The following template will decide if T is abstract with g++. /** isAbstract<T>::result is 1 if T is abstract, 0 if otherwise. */ template<typename T> class isAbstract { class No { }; class Yes { No no[3]; }; template<class U> static No test( U (*)[1] ); // not defined template<class U> static Yes test( ... ); // not d...

How can I use templates to determine the appropriate argument passing method?

As I understand it, when passing an object to a function that's larger than a register, it's preferable to pass it as a (const) reference, e.g.: void foo(const std::string& bar) { ... } This avoids having to perform a potentially expensive copy of the argument. However, when passing a type that fits into a register, passing it as...

How to write `is_complete` template?

After answering this question I was trying to find is_complete template in Boost library and I realized that there is no such template in Boost.TypeTraits. Why there is no such template in Boost library? How it should look like? //! Check whether type complete template<typename T> struct is_complete { static const bool value = ( si...

Generic object carrier class - C++

I need to create a generic object carrier class. I came up with something simple like template<typename T> class ObjectCarrier { public: const T& item() const { return item_; } void setItem(T& item) { item_ = item; } private: T item_; }; This works well when T has got a default constructor (par...

Type traits definition. Traits blobs & Metafunctions.

Reading some source code, I have found next traits definition: namespace dds { template <typename Topic> struct topic_type_support { }; template <typename Topic> struct topic_data_writer { }; template <typename Topic> struct topic_data_reader { }; template <typename Topic> struct topic_data_seq { }; } #define REGISTER_TOP...

Help with type traits

Suppose we have the following template class template<typename T> class Wrap { /* ... */ }; We can not change Wrap. It is important. Let there are classes derived from Wrap<T>. For example, class NewInt : public Wrap<int> { /* ... */ }; class MyClass : public Wrap<myclass> { /* ... */ }; class Foo : public Wrap<Bar> { /...

C++ type traits to check if class has operator/member

hi Is it possible to use boost type traits or some other mechanism to check if a particular template parameter has an operator/function, e.g. std::vector as a template parameter has operator[], while std::pair does not. ...

How `is_base_of` works?

Why the following code works? typedef char (&yes)[1]; typedef char (&no)[2]; template <typename B, typename D> struct Host { operator B*() const; operator D*(); }; template <typename B, typename D> struct is_base_of { template <typename T> static yes check(D*, T); static no check(B*, int); static const bool value = sizeo...

integer traits (is_integer, is_integral)

I need two traits concerning integers. The first one would be like std::is_integral (or boost::is_integral), but usable with user defined types (for example a class wrapping an int, say int_wrapper): true if the type behaves like an integer and whose representation is like standard integral types (e.g. sizeof(T) * CHAR_BITS == std::num...

C++ - type traits question

Hello! I wish to know if it's possible in C++ to somehow handle the following situations: Situation 1) (Easily handled) class BasicFacility { } template <typename U1, typename U2> class Facility : public BasicFacility { } Suppose now that we want to have some compilation-time assertion and we want to check if the arbitrary type ty...