Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a templated T as pointer

Tags:

c++

pointers

I want to define some generic pointer (? but not a void pointer) using this code:

class A
{

template<typename T>
using ptr = T*;

using ubyte = uint8_t;

public:

    const ptr<ubyte>
    getColor1() const {
        return &colors[0];
    }

    const ubyte*
    getColor2() const {
        return &colors[0];
    }


private:

    ubyte colors[4];
};

However, the getColor1() won't compile. What's the difference between this two functions ?

gcc says:

error: invalid conversion from 'const ubyte* {aka const unsigned char*}' to 'A::ptr<unsigned char> {aka unsigned char*}' [-fpermissive]|

Update:

The deleted answer says I could do this:

//option 1
template<typename T>
using const_ptr = const T*;

or

//option 2
const ptr<ubyte>
getColor()  //remove cv-qualifier
{
    return &colors[0];
}

From option1,

It constructs now to const const, what does const const means?

From option2,

Why just removing cv-qualifier makes this compile?

like image 373
mr5 Avatar asked Mar 04 '14 02:03

mr5


People also ask

How do you declare a template function?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>( float original ); Template arguments may be omitted when the compiler can infer them.

Can template type be a pointer?

A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.

What happens when a function is defined as a template?

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.

What is specialization C++?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.


1 Answers

const ptr<ubyte> is the same as const (ubyte *) which is not the same as const ubyte (*). You are trying to return a const pointer to a non-const char, which the compiler won't allow because you've declared the function itself const; all members become const because of that. The compiler won't automatically cast const to non-const without a const_cast.

To make the difference clearer, the first is a const pointer to a non-const char and the second is a non-const pointer to a const char. The first allows the pointed-to characters to change, even though the pointer itself can't change. Since the function was marked as const it can't return anything that would allow its members to be modified.

The best way to fix it:

ptr<const ubyte>
getColor1() const {
    return &colors[0];
}
like image 128
Mark Ransom Avatar answered Sep 26 '22 17:09

Mark Ransom