Say if I had a vector<string> already defined and filled called test and an int called a. If I wanted to combine these 2 into a single object called combined where i could do combined[0] = test; to initialize/retrieve the object with the vector and combined[1] = a; to initialize/retrieve the object with the int, what would be the best function to do so and how would I do so? I had attempted to do vector<vector<string>, int> but this gave me an error.
Note: I am compiling with -std=c++11 if this matters.
Use a std::tuple<std::vector<std::string>,int>.
#include <tuple>
#include <vector>
#include <string>
int main() {
std::vector<std::string> test;
int a{};
std::tuple<std::vector<std::string>,int> combined;
//To access elements, use `std::get`:
std::get<0>(combined) = test;
std::get<1>(combined) = a;
}
to answer cellsheet's comment: that function already exists, it's called std::make_tuple() (see also comment by fjardon on how to store this).
Btw, why do you need to extend std::vector<std::string> by an int?
If I understand correctly what you're asking, I think you can do this with a std::pair:
std::pair<std::vector<std::string>, int> combined;
combined.first = test; // assign vector
combined.second = a; // assign int
or simply
auto combined = std::make_pair(test,a);
It requires (ugly) type elision:
#include <iostream>
#include <stdexcept>
#include <type_traits>
#include <vector>
class X {
public:
typedef std::vector<std::string> vector_type;
typedef int integer_type;
private:
enum Type {
TypeVector,
TypeInteger
};
template <bool Constant>
class Proxy
{
private:
typedef typename std::conditional<
Constant, const void, void>::type void_t;
public:
typedef typename std::conditional<
Constant, const vector_type, vector_type>::type vector_t;
typedef typename std::conditional<
Constant, const integer_type, integer_type>::type integer_t;
Proxy(vector_t& v)
: m_type(TypeVector), m_data(&v)
{}
Proxy(integer_t& i)
: m_type(TypeInteger), m_data(&i)
{}
operator vector_t& () const {
if(m_type != TypeVector) throw std::runtime_error("Invalid Type");
return *static_cast<vector_t*>(m_data);
}
operator integer_t& () const {
if(m_type != TypeInteger) throw std::runtime_error("Invalid Type");
return *static_cast<integer_t*>(m_data);
}
private:
template <typename T, typename U, bool> struct Assignment
{
static void apply(void_t*, const U&) {}
};
template <typename T, typename U>
struct Assignment<T, U, true>
{
static void apply(void_t* p, const U& value) {
*static_cast<T*>(p) = value;
}
};
template <typename T, typename U>
// Attention: Use a reference - std::is_assignable<int, int>::value> is false;
struct Assign : Assignment<T, U, std::is_assignable<T&, U>::value>
{};
public:
template <typename U>
Proxy&
operator = (const U& value) {
static_assert( ! Constant, "Assignment to Constant");
switch(m_type) {
case TypeVector:
Assign<vector_t, U>::apply(m_data, value);
break;
case TypeInteger:
Assign<integer_t, U>::apply(m_data, value);
break;
default: throw std::out_of_range("Invalid Type");
}
return *this;
}
private:
Type m_type;
void_t* m_data;
};
public:
X() : m_v{"Hello"}, m_i(1) {}
Proxy<true> operator [] (std::size_t i) const {
switch(i) {
case 0: return Proxy<true>(m_v);
case 1: return Proxy<true>(m_i);
default: throw std::out_of_range("Invalid Index");
}
}
Proxy<false> operator [] (std::size_t i) {
switch(i) {
case 0: return Proxy<false>(m_v);
case 1: return Proxy<false>(m_i);
default: throw std::out_of_range("Invalid Index");
}
}
private:
vector_type m_v;
integer_type m_i;
};
int main() {
// Note: The Proxy has no operator []
// const
{
const X x;
const X::vector_type& v = x[0];
std::cout << v[0] << " " << x[1] << std::endl;
}
// non const
{
X x;
X::vector_type& v = x[0];
v[0] = "World";
x[1] = 2;
std::cout << v[0] << " " << x[1] << std::endl;
}
}
You might consider boost::any, instead.
Related
I want to declare class which depends on 'mode' field works with different type of standard containers. How I can do that?!
class Container {
public:
Container(int8_t initMode);
void addPair(string name, int32_t number);
int8_t mode;
private:
std::_Container_base _container;
};
Container::Container(int8_t m) {
mode = m >= 0 && m <= 2 ? m : 0;
switch (mode) {
case 0:
//_container should be Map;
break;
case 1:
//_container should be Dictionary;
break;
case 2:
//_container should be HashMap;
break;
}
}
When you declare a variable of a certain type, the variable must be that type exactly. You can instead use polymorphism and make _container a std::_Container_base*, and then allocate it as whatever subclass of std::_Container_base you want via new.
I don't think std::_Container_base is portable, though, so you should consider using a union of the containers you want, then switch which one you use based on the mode.
You could use a variant, you wouldn't even need a mode attribute since std::holds_alternative<MapType>(_container) can give you that info.
template <class T>
class Container {
public:
private:
using Key = std::string;
std::variant<
std::map<Key,T>,
std::multimap<Key,T>,
std::unordered_map<Key,T>,
std::unordered_multimap<Key,T>> _container;
}
https://en.cppreference.com/w/cpp/utility/variant
More complete example:
#include <iostream>
#include <string>
#include <map>
#include <stdexcept>
#include <unordered_map>
#include <variant>
template <class T>
class Container {
public:
using Key = std::string;
template<class C> Container(const C& c) : _container(c) {}
template<class C> Container(C&& c) : _container(std::move(c)) {}
template<class C>
Container& operator=(const C& c) { _container = c; return *this; }
template<class C>
Container& operator=(C&& c) { _container = std::move(c); return *this; }
T getFirstElement(const Key& key) {
if (!hasKey(key)) throw std::runtime_error("No such key: " + key);
return std::visit([this, key](auto& cont){ return cont.find(key)->second; }, _container);
}
T getFirstElement(const Key& key, T or_value) noexcept {
if (!hasKey(key)) return or_value;
return getFirstElement(key);
}
bool hasKey(const Key& key) const {
return std::visit([this, key](auto& cont){ return cont.count(key); }, _container) > 0;
}
private:
std::variant<
std::map<Key,T>,
std::multimap<Key,T>,
std::unordered_map<Key,T>,
std::unordered_multimap<Key,T>> _container;
};
int main()
{
std::map<std::string, int> someMap {{"one", 1}, {"two", 2}};
std::unordered_map<std::string, int> someUmap {{"one", 11}, {"two", 22}};
Container<int> cont(someMap);
std::cout << cont.getFirstElement("two") << std::endl; // 2
cont = someUmap;
std::cout << cont.getFirstElement("two") << std::endl; // 22
return 0;
}
I'm trying to find a [better] way to run/check a potentially unsafe expression or perform multiple null checks in a more elegant way.
Here is an example of codes I would like to improve:
if (myObjectPointer &&
myObjectPointer->getSubObject() &&
myObjectPointer->getSubObject()->getSubSubObject() &&
myObjectPointer->getSubObject()->getSubSubObject()->getTarget()) {
// Use safely target
... *(myObjectPointer->getSubObject()->getSubSubObject()->getTarget()) ...
}
I tried to find a more elegant way to achieve this (instead of the above verbose null checks). Here is my first thoughts:
template<typename T>
bool isSafe(T && function) {
try {
function();
// Just running the func above, but we could e.g. think about returning the actual value instead of true/fase - not that important.
return true;
}
catch (...) {
return false;
}
}
...
// And use the above as follow :
if(isSafe([&](){ myObjectPointer->getSubObject()->getSubSubObject()->getTarget(); })) {
// Use safely target
}
...
The problem with the above is that we can't catch signals (Segmentation fault, ...). And I obviously don't want to handle all signals in the program, but only in this very specific check/eval function.
I'm I tackling the problem the wrong way ? Any other recommendations ? or the verbose if is inevitable ?
Many thanks in advance.
I was thinking about this, and like Jarod42 said, there must be some variadic template stuff. I'm not the best at this, but came up with this:
#include <memory>
#include <functional>
#include <iostream>
template <typename T, typename MemFn, typename... Params>
void safeExecute(T* ptr, MemFn memFn, Params&&... params) {
if (ptr != nullptr)
safeExecute(std::invoke(memFn, ptr), std::forward<Params>(params)...);
}
template <typename T, typename MemFn>
void safeExecute(T* ptr, MemFn memFn) {
if (ptr != nullptr) std::invoke(memFn, ptr);
}
struct Target {
void Bar() { std::cout << "tada!\n"; };
};
template<typename T>
class Object {
private:
std::unique_ptr<T> ptr;
public:
Object() : ptr(std::make_unique<T>()) {}
T* Get() { return ptr.get(); }
};
using SubSubObject = Object<Target>;
using SubObject = Object<SubSubObject>;
using MyObject = Object<SubObject>;
int main() {
auto myObjectPtr = std::make_unique<MyObject>();
safeExecute(myObjectPtr.get(),
&MyObject::Get,
&SubObject::Get,
&SubSubObject::Get,
&Target::Bar);
}
edit:
I've been playing with the idea of having a more general return type, so I experimented with the option not to call the member function, but to return an std::optional pointer to the object. This lead me to the following code:
#include <memory>
#include <functional>
#include <iostream>
#include <optional>
template <typename T, typename MemFn, typename... Params>
auto safeGetObject(T* ptr, MemFn memFn, Params&&... params)
-> decltype(safeGetObject(std::invoke(memFn, std::declval<T>()), std::forward<Params>(params)...))
{
if (ptr != nullptr) return safeGetObject(std::invoke(memFn, ptr), std::forward<Params>(params)...);
return {};
}
template <typename T, typename MemFn>
auto safeGetObject(T* ptr, MemFn memFn) -> std::optional<decltype(std::invoke(memFn, std::declval<T>()))> {
if (ptr != nullptr) return std::invoke(memFn, ptr);
return {};
}
struct Target {
int Bar(int a, int b) const noexcept {
return a+b;
};
};
template<typename T>
class Object {
private:
std::unique_ptr<T> ptr;
public:
Object() noexcept : ptr(std::make_unique<T>()) {}
T* Get() const noexcept { return ptr.get(); }
};
using SubSubObject = Object<Target>;
using SubObject = Object<SubSubObject>;
using MyObject = Object<SubObject>;
int main() {
auto myObjectPtr = std::make_unique<MyObject>();
auto optionalTarget = safeGetObject(
myObjectPtr.get(),
&MyObject::Get,
&SubObject::Get,
&SubSubObject::Get);
auto result = optionalTarget ? optionalTarget.value()->Bar(3, 4) : -1;
std::cout << " result " << result << '\n';
}
Putting possible design issues aside, you could use an extended version of std::optional. Since not all intefaces are under your control, you would have to wrap the functions were necessary into a free-function. Let's assume you can change the class MyClass of myObjectPointer, but not the classes of the sub-objects.
class MyClass {
public:
optional<std::reference_wrapper<SubObjectClass>> getSubObject();
};
optional<std::reference_wrapper<SubSubObjectClass>> getSubSubObject(SubObjectClass& s) {
SubSubObjectClass* ptr = s.getSubSubObject();
if (ptr) {
return std::ref(s.getSubSubObject());
} else {
return {};
}
}
optional<std::reference_wrapper<Target>> getTarget(SubSubObjectCLass& s) {
...
}
You can now write something like
optional<MyClass*> myObjectPointer = ...;
myObjectPointer.and_then(MyClass::getSubObject)
.and_then(getSubSubObject)
.and_then(getTarget)
.map( doSomethingWithTarget ):
OK, I might delete my previous answer, because I've been rethinking this, now considering using std::optional and chaining.
Your original
myObjectPointer->getSubObject()->getSubSubObject()->getTarget()
is not really reproducible, since operator->() cannot be static. But we can use another operator, like operator>>(). Thus:
#include <memory>
#include <iostream>
#include <optional>
#include <functional>
struct Target {
int Bar(int a, int b) const noexcept { return a+b; };
};
template<typename T>
class Object {
private:
T* const ptr;
public:
Object(T* ptr) noexcept : ptr(ptr) {}
T* Get() const noexcept { return ptr; }
};
using SubSubObject = Object<Target>;
using SubObject = Object<SubSubObject>;
using MyObject = Object<SubObject>;
template <typename T>
auto makeOptional(T* ptr) -> std::optional< std::reference_wrapper<T>> {
if (ptr) return std::ref(*ptr);
return {};
}
template <typename T, typename MemFn>
auto operator>> (std::optional<std::reference_wrapper<T>> optObj, MemFn memFn)
-> std::optional< std::reference_wrapper<std::remove_pointer_t<decltype(std::invoke(memFn, std::declval<T>()))>>> {
if (optObj) return makeOptional(std::invoke(memFn, *optObj));
return {};
}
int main() {
{
//complete
auto TargetPtr = std::make_unique<Target>();
auto subSubObjectPtr = std::make_unique<SubSubObject>(TargetPtr.get());
auto subObjectPtr = std::make_unique<SubObject>(subSubObjectPtr.get());
auto myObjectPtr = std::make_unique<MyObject>(subObjectPtr.get());
auto optionalMyObject = makeOptional(myObjectPtr.get());
auto optionalTarget = optionalMyObject >> &MyObject::Get >> &SubObject::Get >> &SubSubObject::Get;
auto result = (optionalTarget) ? optionalTarget->get().Bar(3, 4) : -1;
std::cout << "result is " << result << '\n';
}
{
// incomplete
auto subObjectPtr = std::make_unique<SubObject>(nullptr);
auto myObjectPtr = std::make_unique<MyObject>(subObjectPtr.get());
auto optionalMyObject = makeOptional(myObjectPtr.get());
auto optionalTarget = optionalMyObject >> &MyObject::Get >> &SubObject::Get >> &SubSubObject::Get;
auto result = (optionalTarget) ? optionalTarget->get().Bar(3, 4) : -1;
std::cout << "result is " << result << '\n';
}
}
will work...
Let me know if this is what you're looking for.
edit:
I've also tried putting it in a wrapper class
#include <memory>
#include <iostream>
#include <functional>
#include <optional>
struct Target {
constexpr int Bar(int a, int b) const noexcept { return a + b; };
};
template<typename T>
class Object {
private:
T* const ptr;
public:
constexpr Object(T* const ptr) noexcept : ptr(ptr) {}
constexpr T* Get() const noexcept { return ptr; }
};
using SubSubObject = Object<Target>;
using SubObject = Object<SubSubObject>;
using MyObject = Object<SubObject>;
template<typename T>
class ObjectWrapper {
private:
std::optional<std::reference_wrapper<T>> optRefObj{};
public:
constexpr ObjectWrapper(T* ptr) noexcept
: optRefObj(ptr ? std::make_optional(std::ref(*ptr)) : std::nullopt)
{}
template<typename MemFn>
constexpr auto operator>>(MemFn memFn) const noexcept {
return ObjectWrapper<std::remove_pointer_t<decltype(std::invoke(memFn, std::declval<T>()))>>
(optRefObj ? std::invoke(memFn, *optRefObj) : nullptr);
}
constexpr operator bool() const noexcept { return optRefObj.has_value(); }
constexpr T* Get() noexcept { return optRefObj ? &optRefObj->get() : nullptr; }
};
int main() {
{
//complete
auto const TargetPtr = std::make_unique<Target>();
auto const subSubObjectPtr = std::make_unique<SubSubObject>(TargetPtr.get());
auto const subObjectPtr = std::make_unique<SubObject>(subSubObjectPtr.get());
auto const myObjectPtr = std::make_unique<MyObject>(subObjectPtr.get());
auto const myObjWrp = ObjectWrapper(myObjectPtr.get());
auto optionalTarget = myObjWrp >> &MyObject::Get >> &SubObject::Get >> &SubSubObject::Get;
auto const result = optionalTarget ? optionalTarget.Get()->Bar(3, 4) : -1;
std::cout << "result is " << result << '\n';
}
{
// incomplete
auto const subObjectPtr = std::make_unique<SubObject>(nullptr);
auto const myObjectPtr = std::make_unique<MyObject>(subObjectPtr.get());
auto const myObjWrp = ObjectWrapper(myObjectPtr.get());
auto optionalTarget = myObjWrp >> &MyObject::Get >> &SubObject::Get >> &SubSubObject::Get;
auto const result = optionalTarget ? optionalTarget.Get()->Bar(3, 4) : -1;
std::cout << "result is " << result << '\n';
}
}
I want to make template specializations for classes with unknown name, I only know their base, and i want all derived classes to fall onto the specialized template functions.
The following code is a working one for Case 1-4. I couldn't make Case 5 to work.
Also this is mostly runtime checking with the is_base_of, a much more effective one would be a compile time solution, but i failed at that. :(
#include <typeinfo>
#include <iostream>
struct MyClass {};
struct Rnd : public MyClass {};
void* JustAVoidPointer=new int;
void* AnOtherPointer = new int;
struct A
{
template <typename T> operator T() const {
if (std::is_base_of<MyClass, T>::value == true)
{
JustAVoidPointer = (MyClass*)(new T);
std::cout << "Case 3"; return *((T*)(JustAVoidPointer));
}
std::cout << "Case 1"; return *((T*)AnOtherPointer);
}
//template <typename T> operator T&() {
// std::cout << "Case 5"; return *(this->operator T* ());
//}
template <typename T> operator T*() const {
if (std::is_base_of<MyClass, T>::value == true)
{
JustAVoidPointer = (MyClass*) new T;
std::cout << "Case 4"; return (T*)(JustAVoidPointer);
}
std::cout << "Case 2"; return *((T**)AnOtherPointer);
}
}a;
void main()
{
auto CantUseTheStructsName = [&]() {
struct Rnd : public MyClass {};
int z = a; //Case 1
int* w = a; //Case 2
Rnd x = a; //Case 3
Rnd* y = a; //Case 4
//Rnd& z=a; //Case 5
char* xx=0; std::cin >> xx;
}; CantUseTheStructsName();
}
I would appreciate any help or advice what you could give me, Thank you!:)
I have a feeling that it's impossible to differentiate between a reference operator template and a non reference one, the reference one always takes precedence if we provide both.
A workaround, and a compile time solution:
#include <typeinfo>
#include <iostream>
struct MyClass {};
struct Rnd : public MyClass {};
void* JustAVoidPointer=0;
void* AnOtherPointer = new int;
struct A
{
template <typename T, typename F = T*> operator T&() const {
return *((T*)CheckType(F{}, std::is_pointer<T>{}, std::is_base_of<MyClass, std::remove_pointer<T>::type>{}));
}
template <typename T> T* CheckType(T*, std::false_type, std::false_type) const {
std::cout << " Case 1";
return ((T*)AnOtherPointer);
}
template <typename T> T* CheckType(T*, std::true_type, std::false_type) const {
std::cout << " Case 2";
return ((T*)AnOtherPointer);
}
template <typename T> T* CheckType(T*, std::false_type, std::true_type) const {
std::cout << " Case 3";
JustAVoidPointer = (MyClass*)(new T);
return ((T*)JustAVoidPointer);
}
template <typename T> T* CheckType(T*, std::true_type, std::true_type) const {
std::cout << " Case 4";
JustAVoidPointer = (MyClass*)(new std::remove_pointer<T>::type);
return ((T*)(&JustAVoidPointer));
}
}a;
void main()
{
auto CantUseTheStructsName = [&]() {
struct Rnd : public MyClass {};
int x = a; //Case 1
int& y = a; //Case 1
int* z = a; //Case 2
Rnd j = a; //Case 3
Rnd& k = a; //Case 3
Rnd* f = a; //Case 4
char* xx=0; std::cin >> xx;
}; CantUseTheStructsName();
}
I'm studying c++ templates and reading <<C++ Templates: The Complete Guide>>. I don't understand the flowing about expression template:
The code as following:
//exprarray.h
#include <stddef.h>
#include <cassert>
#include "sarray.h"
template<typename T>
class A_Scale
{
public:
A_Scale(T const& t):value(t){}
T operator[](size_t) const
{
return value;
}
size_t size() const
{
return 0;
}
private:
T const& value;
};
template<typename T>
class A_Traits
{
public:
typedef T const& exprRef;
};
template<typename T>
class A_Traits<A_Scale<T> >
{
public:
typedef A_Scale<T> exprRef;
};
template<typename T,typename L1,typename R2>
class A_Add
{
private:
typename A_Traits<L1>::exprRef op1;
typename A_Traits<R2>::exprRef op2;
public:
A_Add(L1 const& a,R2 const& b):op1(a),op2(b)
{
}
T operator[](size_t indx) const
{
return op1[indx] + op2[indx];
}
size_t size() const
{
assert(op1.size()==0 || op2.size()==0 || op1.size() == op2.size());
return op1.size() != 0 ? op1.size() : op2.size();
}
};
template<typename T,typename L1,typename R2>
class A_Mul
{
private:
typename A_Traits<L1>::exprRef op1;
typename A_Traits<R2>::exprRef op2;
public:
A_Mul(L1 const& a,R2 const& b):op1(a),op2(b)
{
}
T operator[](size_t indx) const
{
return op1[indx] * op2[indx];
}
size_t size() const
{
assert(op1.size()==0 || op2.size()==0 || op1.size() == op2.size());
return op1.size() != 0 ? op1.size():op2.size();
}
};
template<typename T,typename Rep = SArray<T> >
class Array
{
public:
explicit Array(size_t N):expr_Rep(N){}
Array(Rep const& rep):expr_Rep(rep){}
Array& operator=(Array<T> const& orig)
{
assert(size() == orig.size());
for (size_t indx=0;indx < orig.size();indx++)
{
expr_Rep[indx] = orig[indx];
}
return *this;
}
template<typename T2,typename Rep2>
Array& operator=(Array<T2,Rep2> const& orig)
{
assert(size() == orig.size());
for (size_t indx=0;indx<orig.size();indx++)
{
expr_Rep[indx] = orig[indx];
}
return *this;
}
size_t size() const
{
return expr_Rep.size();
}
T operator[](size_t indx) const
{
assert(indx < size());
return expr_Rep[indx];
}
T& operator[](size_t indx)
{
assert(indx < size());
return expr_Rep[indx];
}
Rep const& rep() const
{
return expr_Rep;
}
Rep& rep()
{
return expr_Rep;
}
private:
Rep expr_Rep;
};
template<typename T,typename L1,typename R2>
Array<T,A_Add<T,L1,R2> >
operator+(Array<T,L1> const& a,Array<T,R2> const& b)
{
return Array<T,A_Add<T,L1,R2> >(A_Add<T,L1,R2>(a.rep(),b.rep()));
}
template<typename T,typename L1,typename R2>
Array<T,A_Mul<T,L1,R2> >
operator*(Array<T,L1> const& a,Array<T,R2> const& b)
{
return Array<T,A_Mul<T,L1,R2> >(A_Mul<T,L1,R2>(a.rep(),b.rep()));
}
template<typename T,typename R2>
Array<T,A_Mul<T,A_Scale<T>,R2> >
operator*(T const& a,Array<T,R2> const& b)
{
return Array<T,A_Mul<T,A_Scale<T>,R2> >(A_Mul<T,A_Scale<T>,R2>(A_Scale<T>(a),b.rep()));
}
The test code:
//test.cpp
#include "exprarray.h"
#include <iostream>
using namespace std;
template <typename T>
void print (T const& c)
{
for (int i=0; i<8; ++i) {
std::cout << c[i] << ' ';
}
std::cout << "..." << std::endl;
}
int main()
{
Array<double> x(1000), y(1000);
for (int i=0; i<1000; ++i) {
x[i] = i;
y[i] = x[i]+x[i];
}
std::cout << "x: ";
print(x);
std::cout << "y: ";
print(y);
x = 1.2 * x;
std::cout << "x = 1.2 * x: ";
print(x);
x = 1.2*x + x*y;
std::cout << "1.2*x + x*y: ";
print(x);
x = y;
std::cout << "after x = y: ";
print(x);
return 0;
}
My questions is why A_Traits for A_Scale is by value not by reference.
template<typename T>
class A_Traits
{
public:
typedef T const& exprRef;
};
template<typename T>
class A_Traits<A_Scale<T> >
{
public:
typedef A_Scale<T> exprRef;
};
The reason from the book as following:
This is necessary because of the following: In general, we can declare them to be references because most temporary nodes are bound in the top-level expression and therefore live until the end of the evaluation of that complete expression. The one exception are the A_Scalar nodes. They are bound within the operator functions and might not live until the end of the evaluation of the complete expression. Thus, to avoid that the members refer to scalars that don't exist anymore, for scalars the operands have to get copied "by value."
More detail please refer to the chapter 18 of C++ Templates: The Complete Guide
Consider, for example, the right hand side of
x = 1.2*x + x*y;
What the quote says is that this is composed of two different categories.
The heavy array x and y objects are not defined within this expression, but rather before it:
Array<double> x(1000), y(1000);
So, as you build expressions using them, you don't have to worry whether they're still alive - they were defined beforehand. Since they're heavy, you want to capture them by reference, and, fortunately, their lifetime makes that possible.
Conversely, the lightweight A_Scale objects are generated within the expression (e.g., implicitly by the 1.2 above). Since they're temporaries, you have to worry about their lifetime. Since they're lightweight, it's not a problem.
That's the rationale for the traits class differentiating between them: the former are by reference, and the latter are by value (they are copied).
The following code yields warning in G++:
#include <iostream>
#include <cstdint>
template <typename T, typename P, typename Q>
Q T::*pointer_to(P T::*p, Q P::*q)
{
typedef Q T::* output_ptr;
// warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
size_t tmp = reinterpret_cast<const size_t&>(p) + reinterpret_cast<const size_t&>(q);
return reinterpret_cast<const output_ptr&>(tmp);
}
struct A { int x; };
struct B { A a; };
int main()
{
B b = B();
b.*pointer_to(&B::a, &A::x) = 1;
std::cout << b.a.x << std::endl;
}
It works properly anyway, but that makes me worry.
What is your opinion, are these "sub-member" pointers susceptible to extra strict aliasing issues than plain member pointers?
I would recommend against doing it this way.
You stated in your comments that you tried using a nested std::bind, but there's an issue with the compiler version you're using. Rather than resort to the hack, I would roll my own repeated pointer to member class.
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <utility>
template<typename Ptr1, typename... Rest>
class pointer_to_sub;
template<typename ObjType, typename Class>
class pointer_to_sub<ObjType Class::* >
{
typedef ObjType Class::* ptr_type;
public:
typedef ObjType value_type;
typedef Class input_type;
pointer_to_sub(ptr_type input) : ptr(input)
{
}
value_type& operator()(input_type& from) const
{
return from.*ptr;
}
value_type const& operator()(input_type const& from) const
{
return from.*ptr;
}
value_type& operator()(input_type* from) const
{
return from->*ptr;
}
value_type const& operator()(input_type const* from) const
{
return from->*ptr;
}
private:
ptr_type ptr;
};
template<typename ObjType, typename Class, typename... Rest >
class pointer_to_sub<ObjType Class::*, Rest...> : private pointer_to_sub<Rest...>
{
typedef ObjType Class::* ptr_type;
typedef pointer_to_sub<Rest...> base_type;
public:
typedef typename base_type::value_type value_type;
typedef Class input_type;
pointer_to_sub(ptr_type input, Rest... args) : base_type(args...), ptr(input)
{
}
value_type& operator()(input_type& from) const
{
return base_type::operator()(from.*ptr);
}
value_type const& operator()(input_type const& from) const
{
return base_type::operator()(from.*ptr);
}
value_type& operator()(input_type* from) const
{
return base_type::operator()(from->*ptr);
}
value_type const& operator()(input_type const* from) const
{
return base_type::operator()(from->*ptr);
}
private:
ptr_type ptr;
};
template<typename T, typename... Args>
pointer_to_sub<T, Args...> make_pointer_to_sub(T t1, Args... args)
{
return pointer_to_sub<T, Args...>(t1, args...);
}
The above basically provides a make_pointer_to_sub which takes a list of member object pointers. It accepts as its input a reference or a pointer that's convertible to the first type, and then dereferences each of the pointers in turn. It could be improved to accept unique_ptr or shared_ptr, but that's for later. You use it as seen below.
struct A { int x; double y;};
struct B { A a; };
int main()
{
auto ptr = make_pointer_to_sub(&B::a, &A::x);
B b = B();
ptr(b) = 1;
// b.*pointer_to(&B::a, &A::x) = 1;
std::cout << b.a.x << std::endl;
ptr(&b) = 2;
std::cout << b.a.x << std::endl;
}
If you needed to, this could be assigned to a std::function with the appropriate arguments.