Identifying Primitive Types in Templates

Identifying primitive types in templates

UPDATE: Since C++11, use the is_fundamental template from the standard library:

#include <type_traits>

template<class T>
void test() {
if (std::is_fundamental<T>::value) {
// ...
} else {
// ...
}
}

// Generic: Not primitive
template<class T>
bool isPrimitiveType() {
return false;
}

// Now, you have to create specializations for **all** primitive types

template<>
bool isPrimitiveType<int>() {
return true;
}

// TODO: bool, double, char, ....

// Usage:
template<class T>
void test() {
if (isPrimitiveType<T>()) {
std::cout << "Primitive" << std::endl;
} else {
std::cout << "Not primitive" << std::endl;
}
}

In order to save the function call overhead, use structs:

template<class T>
struct IsPrimitiveType {
enum { VALUE = 0 };
};

template<>
struct IsPrimitiveType<int> {
enum { VALUE = 1 };
};

// ...

template<class T>
void test() {
if (IsPrimitiveType<T>::VALUE) {
// ...
} else {
// ...
}
}

As others have pointed out, you can save your time implementing that by yourself and use is_fundamental from the Boost Type Traits Library, which seems to do exactly the same.

Limiting C++ templates to primitive types

(note: requires C++11)

#include <type_traits>

template <class T>
class ModelParameter
{
static_assert(std::is_fundamental<T>::value, "error message");
...
};

But why would you want to do this?

How to condition a class template and primitive type template

This is where a template specialization comes in handy.

#include <type_traits>

template <class Class_, bool = std::is_class<Class_>::value>
struct EndianGuard_ : public Class_
{
// inherit if class
};

template <class Class_>
struct EndianGuard_<Class_, false>
{
// don't inherit if not a class
};

struct foo {};

int main()
{
EndianGuard_<int> f; //ok
EndianGuard_<foo> f2; // ok
}

If std::is_class<Class_>::value evaluates to false, the specialization kicks in and you can handle the non-class type. Other conditions can be added for more restriction.

As a side note, it's probably best to handle the std::is_final case, since you can't inherit from a final class.

Creating templates in C++ to handle pointers to objects and primitive types

Use a partial template specialization:

Primary template:

template<typename T>
struct Foo
{
bool operator ==( T otherData )
{
return m_data == otherData;
}
T m_data;
};

Partial template specialization for T*

template<class T>
struct Foo<T*>
{
bool operator ==( const T &otherObj )
{
return m_obj->compare( otherObj );
}
T* m_obj;
};

Is there a way to specialize a template to target primitives?

You can used C++11 type_traits. Here is something to get you started, you can specialize more as needed:

#include <type_traits>
#include <iostream>

template<typename T, typename E = void>
struct A; // undefined

template<typename T>
struct A<T, typename std::enable_if<std::is_class<T>::value && !std::is_pod<T>::value>::type> {
A() { std::cout << "I'm a class, but I'm not a pod type" << std::endl; }
};

template<typename T>
struct A<T, typename std::enable_if<std::is_class<T>::value && std::is_pod<T>::value>::type> {
A() { std::cout << "I'm a class and a pod type" << std::endl; }
};

template<typename T>
struct A<T, typename std::enable_if<!std::is_class<T>::value>::type> {
A() { std::cout << "I'm not a class" << std::endl; }
};

class X {};
class Y { ~Y(){} };

int main()
{
A<X> a1;
A<Y> a2;
A<int> a3;
}

MVC3 Razor custom templates for primitive types and downcasting

It's because EditorFor uses compile-time binding on the generic parameters to determine which template to render.

And since there's no EditorFor<object>() defined by default, you get nothing.

I believe you can define your own editor template for object if you want, but probably a better way would be to make your view model generic:

public class AttributeEditorViewModel<TValue>
{
public long AttributeId { get; set; }
public string DisplayName { get; set; }
public TValue Value { get; set; }
}

However, this might not be ideal as you could no longer have a list of polymorphic view models.

Java: How to determine if type is any of primitive/wrapper/String, or something else

I found something:

Commons Lang: (would have to combine with check for String)

ClassUtils.isPrimitiveOrWrapper()

Spring:

BeanUtils.isSimpleValueType()

This is what I want, but would like to have it in Commons.



Related Topics



Leave a reply



Submit