2

I have a class called Thing. I make a vector of shared_ptr<Thing>>. Now I want to pass it to a function or something like this: const vector<shared_ptr<const Thing>>.

The code below compiles but I'd prefer to avoid using reinterpret_cast for type safety and because I don't know how this might affect the behavior of the smart pointers.

#include <memory>
#include <vector>

using namespace std;

class Thing {};

typedef vector<shared_ptr<Thing>>             VectorOfThings;
typedef const vector<shared_ptr<const Thing>> VectorOfConstThings;

int main() {
    VectorOfThings *things;
    auto           constThings = reinterpret_cast<VectorOfConstThings *> (things);
}
Michael
  • 1,263
  • 1
  • 12
  • 28

2 Answers2

4

This has nothing to do with smart pointers; the C++ standard doesn't allow conversions between container<T> and container<T const>. This has been talked about before: Why is a vector of pointers not castable to a const vector of const pointers?

It's likely that casting conainter<T> to container<const T> is undefined behavior.

Karl
  • 161
  • 4
  • Right, I overlooked the fact that the type of the contained elements where different (`Thing` and `const Thing`). – JFMR Jan 26 '18 at 07:33
0

I wrote a macro to handle the type conversions. Hopefully, this will eliminate the errors that come from writing all that boiler plate code.

TypeHelper.h

#ifndef TYPE_HELPER_H
#define TYPE_HELPER_H

#include <memory>

/**
 * Creates a struct that has two typedefs and has several functions for reinterpret casting the regular type as
 * the constant type
 * @param NAME The name of the generated struct
 * @param REGULAR_TYPE The typedef of the non-constant type
 * @param CONSTANT_TYPE The typedef of the constant type
 * @param ... The template parameters used in REGULAR_TYPE and CONSTANT_TYPE.
 * There must be at least one parameter passed in. Even if the typedefs don't actually use them.
 * TODO If the typedefs don't need a template parameter, the user should not have to supply a dummy one.
 * TODO Implement some kind of checking to make sure that constness is the only difference.
 * TODO what happens if the template is specialized for a const of that type?
 */
#define TYPE_HELPER( NAME, REGULAR_TYPE, CONSTANT_TYPE, ... ) \
template< __VA_ARGS__ > \
struct NAME { \
typedef REGULAR_TYPE regular; \
typedef CONSTANT_TYPE constant; \
static inline constant *cast( regular *pointer ) { return reinterpret_cast<constant *> (pointer); } \
static inline constant &cast( regular &reference ) { return *reinterpret_cast<constant *> (&reference); }  \
static inline std::unique_ptr<constant> cast( std::unique_ptr<regular> unique_ptr ) { \
    return std::move( *( reinterpret_cast<std::unique_ptr<constant> *> (&unique_ptr))); \
} \
static inline std::shared_ptr<constant> cast( std::shared_ptr<regular> shared_ptr ) { \
    return std::move( *( reinterpret_cast<std::shared_ptr<constant> *> (&shared_ptr))); \
} \
};


#endif //TYPE_HELPER_H

In this file, I used the macro to define a wrapper around the stl vector class

#ifndef VECTOR_TYPE_H
#define VECTOR_TYPE_H

#include <vector>
#include "TypeHelper.h"

TYPE_HELPER( VectorType,
             std::vector<std::shared_ptr<T>>,
             const std::vector<std::shared_ptr<const T>>,
             typename T );

#endif //VECTOR_TYPE_H

And finally, here I use the types

#include "VectorType.h"

//This is a vector of std::shared_ptr<std::string>
VectorType<std::string>::regular strings;

//This is a const vector of std::shared_ptr<const str::string>
//note that I needed to use the cast function to assign the regular one to the constant one
VectorType<std::string>::constant constStrings = VectorType<std::string>::cast(strings);

// You don't have to actually type it twice. Use auto.
auto autoConstStrings = VectorType<std::string>::cast(strings);
Michael
  • 1,263
  • 1
  • 12
  • 28