C++ way to assign an array of char to a T* in a const method of a class

InsideLoop

I would like to use some memory on the stack to store some objects (it arises in a small vector optimization library). Therefore, my class is

template <typename T, int n>
class SmallVector {
private:
    T* begin_;
    T* end_;
    T* capacity_;
    alignas(T) char data_small_[n * sizeof(T)];
public:
    ...
}

To check if the small_data_ buffer is used, I define the function

bool is_data_small_used() const {
    return begin_ == reinterpret_cast<T*>(data_small_);
}

Unfortunately, it does not work. Clang version

Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

gives me the following error message:

./il/container/SmallVector.h:44:25: error: reinterpret_cast from 'const char *' to 'il::Vector<double> *' casts away qualifiers
        return begin_ == reinterpret_cast<T*>(data_small_);

and the Intel compiler says the same. The only solution I found is to do

begin_ == (T*) data_small_

which is not very C++. Is there a "correct way" to do this in C++?

M.M

The error message suggests that the problem is occurring inside a const member function. In that situation, this is considered to point to a const object, so data_small_ will have type const char[N].

A simple fix would be to write:

return begin_ == reinterpret_cast<T const *>(data_small_); 

and another one would be:

return reinterpret_cast<char const *>(begin_) == data_small_;

The C-style cast worked because that cast can do a reinterpret_cast and a const_cast together, whereas a reinterpret_cast by itself cannot cast away const.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to assign values to const char* array and print to screen

From Dev

C assign string from argv[] to char array

From Dev

Global const char pointer array in C++

From Dev

In C, why can't I assign a string to a char array after it's declared?

From Dev

C complains about passing char** value to function taking char const*const*const but C++ doesn't

From Dev

c++: why can't we convert char ** to const char **

From Dev

Cgo: can't find way to use callbacks with const char* argument

From Dev

C# Add extension Append method to class array of type T

From Dev

Assign value to char in array

From Dev

Assign value to char in array

From Dev

Best way to portably assign the result of fgetc() to a char in C

From Dev

Initialize a const char* Array

From Dev

Right way to use const char* as map key in C++

From Dev

Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

From Dev

Why can´t we assign a new string to an char array, but to a pointer?

From Dev

Why is a class member of type char array suddenly of type const char array in member function?

From Dev

C++ const char* with string literal in method call

From Dev

Can't std::ostream output a const char array?

From Dev

Passing a char array as parameter in method, C language

From Dev

Assign value to php class const

From Dev

c++ const char* to char*

From Dev

C convert const char * to char

From Dev

Assign a pointer in a char array to each word in a string C

From Dev

c++ char array output in class functions

From Dev

assigning a const char array to a char array

From Dev

Modify const char * in C

From Dev

Redefine const char* in C

From Dev

fastest way to compare unsigned char* and const char*

From Dev

Assign a function as a class method

Related Related

  1. 1

    How to assign values to const char* array and print to screen

  2. 2

    C assign string from argv[] to char array

  3. 3

    Global const char pointer array in C++

  4. 4

    In C, why can't I assign a string to a char array after it's declared?

  5. 5

    C complains about passing char** value to function taking char const*const*const but C++ doesn't

  6. 6

    c++: why can't we convert char ** to const char **

  7. 7

    Cgo: can't find way to use callbacks with const char* argument

  8. 8

    C# Add extension Append method to class array of type T

  9. 9

    Assign value to char in array

  10. 10

    Assign value to char in array

  11. 11

    Best way to portably assign the result of fgetc() to a char in C

  12. 12

    Initialize a const char* Array

  13. 13

    Right way to use const char* as map key in C++

  14. 14

    Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

  15. 15

    Why can´t we assign a new string to an char array, but to a pointer?

  16. 16

    Why is a class member of type char array suddenly of type const char array in member function?

  17. 17

    C++ const char* with string literal in method call

  18. 18

    Can't std::ostream output a const char array?

  19. 19

    Passing a char array as parameter in method, C language

  20. 20

    Assign value to php class const

  21. 21

    c++ const char* to char*

  22. 22

    C convert const char * to char

  23. 23

    Assign a pointer in a char array to each word in a string C

  24. 24

    c++ char array output in class functions

  25. 25

    assigning a const char array to a char array

  26. 26

    Modify const char * in C

  27. 27

    Redefine const char* in C

  28. 28

    fastest way to compare unsigned char* and const char*

  29. 29

    Assign a function as a class method

HotTag

Archive