How to disable pointers as template typenames

remove pointer
type_traits
std::is_pointer
std::is_same
template pointer c++
std::remove_pointer
enable_if
std remove pointer from type

I would like to make it impossible to instantiate the following class when a pointer is used as the template typename:

template <typename T>
class MyClass{
//...
T payload;
//...
};

So MyClass<int> is fine but MyClass<int*> is not.

It would be wonderful if I can prohibit the instantiation of the class with a struct that has a pointer in it.

There are a couple ways you can do this. You can use SFINAE to constrain the template to non-pointer types like

template <typename T, std::enable_if_t<!std::is_pointer_v<T>, bool> = true>
class MyClass{
    //...
    T payload;
    //...
};

But this can give some pretty hard to understand compiler errors. Using a static_assert you can add your own custom error message like

template <typename T>
class MyClass {
    //...
    static_assert(!std::is_pointer_v<T>, "MyClass<T> requires T to be a non pointer type");
    T payload;
    // ...
};

remove_pointer - C++ Reference, template <class T> struct remove_pointer;. Remove pointer. Obtains the type pointed by T (if T is a pointer). The transformed type is aliased as member type� When myintptr is defined with an int* template parameter, the compiler sees that we have defined a partially specialized template class that works with any pointer type, and instantiates a version of Storage using that template. The constructor of that class makes a deep copy of parameter x.

You can use static_assert + std::is_pointer_v:

template <typename T>
class MyClass {
    static_assert(!std::is_pointer_v<T>);
    // ...
};

C++14 FAQs, 0.32. unique ptr vs make unique as function argument 27 • pointer modifications: template < class T = using remove pointer_t = typename remove pointer. The problem is that indexOf() simply doesn't work for pointer types they way it does for other types. I don't understand your statement. Do you think it is a good way, forcing developers to use const_cast to remove const-ness from a value that is passed to a const argument?

If you don't have C++11 to use std::is_pointer and static_assert, you can define a specialization and leave it undefined:

template <typename T>
class MyClass {

};

template<class T>
class MyClass<T*>; // Requires non-pointer types

template<class T>
class MyClass<T* const>; // Requires non-pointer types

template<class T>
class MyClass<T* const volatile>; // Requires non-pointer types

template<class T>
class MyClass<T* volatile>; // Requires non-pointer types

int main() {
    MyClass<int> mc1;  // Works fine
    MyClass<int*> mc2; // Error
}

13.8 — Partial template specialization for pointers, In previous lesson 13.5 -- Function template specialization, we took a look at a great for Storage<char*>, what about other pointer types (such as int*)? It's 3) I' ve updated the example to remove the strcpy_s since it was� A feature introduced in Word 2002 and available in Word 2013 is click-and-type. In a blank document, you can use it to click the mouse anywhere on the page and type information at that spot. Bam! It’s difficult to see any value in click-and-type, especially when it’s easier just to learn basic formatting. But click-and-type …

It would be wonderful if I can prohibit the instantiation of the class with a struct that has a pointer in it.

This is not possible in C++.

Note that smart pointers are struct that have pointers in them; std::is_pointer does not recognise them so iif you want to prohibit them, you need to provide a separate meta-function (not very hard).

Effective C++ Digital Collection: 140 Ways to Improve Your Programming, pointers that perform reference counting, the remove-related difficulties wash away, and you can use the erase-remove idiom directly: template<typename T>� The only exceptions are that non-type template parameters of reference or pointer type and non-static data members of reference or pointer type in a non-type template parameter of class type and its subobjects (since C++20) cannot refer to/be the address of a subobject (including non-static class member, base subobject, or array element);

Advanced Metaprogramming in Classic C++, This is the SFINAE principle. template <typename T> typename T::pointer f(T*); you want to “enable” or “disable” a particular overload of a function template,� A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename. In short, if the compiler can't tell if a dependent name is a value or a type, then it will assume that it is a value.

std::enable_if, Type support (basic types, RTTI, type traits) to leverage SFINAE to conditionally remove functions from overload resolution based on type traits template<bool B, class T = void> struct enable_if {}; template<class T> struct� In C++, a pointer is a variable that contains the address of an object in the computer’s internal memory. Use these steps to avoid problems with pointers in C++: Initialize pointers when declared. Never leave pointer variables uninitialized – things wouldn’t be too bad if uninitialized pointers always contained random values – the vast majority …

Template argument deduction, template<typename To, typename From> To convert(From f); void g(double d) { int i If removing references and cv-qualifiers from P gives If P is a function type, pointer to function type, or pointer to member function type� The evolution of pointers in C++. Pointers are part of C++ since the first beginning. We got them from C. From the first beginning there was always the tendency in C++ to make the handling with pointers more type-safe without paying the extra cost. With C++98 we got std::auto_ptr to express exclusive ownership. But std::auto_ptr had a big issue.

Comments