1

How to implement a template that gets a class and a pointer to a member of this class?

I want to template a sort of Accessor of the form

template<??? T, ??? P>
class Accessor
{
    // ...

    T& setMember(const Vector3d& point)
    {
        T& obj = getObject<T>();
        obj.*P = point;
        return obj;
    }
};

Here, there is a class T with a member P, both template variables. I know that P is always of type Vector3d. But I have to stay within C++17. Of course, if you have a C++20 technique... I want to know!

Also, Accessor does not have any reference to a T object. But the function getObject magically handles it here. Therefore do not need to care here.

I'd like to use it like this:

class Accessed
{
    Vector3d one_point;
    Vector3d another_point;

    class OneAccessor : public Accessor<&one_point> {};
    class AnotherAccessor : public Accessor<&another_point> {};
}

Notice the format : public Accessor<&one_point>.

Maybe public Accessor<&Accessed::one_point>.

I would prefer

public Accessor<one_point> // or 
public Accessor<Accessed::one_point>

without the &. I'd like Accessed:: to be inferred.

If you have an answer to my problem that you want to deliver, I would very much like to see it.

6
  • The design seems overly complicated at first glance. How do you want to use the Accessed class and OneAccessor/AnotherAccessor classes?
    – Mikhail
    Commented Mar 14, 2023 at 18:27
  • You can use Accessor<&Accessed::one_point> but then where will the Accessed pointer come from when you call setMember? Are you willing to pass the Accessed* to the Accessor constructor? I suspect that if we saw the larger picture, we'd want to solve it a completely different way. Commented Mar 14, 2023 at 18:29
  • @JohnZwinck: No, I do not want to pass it to the constructor. Please, take it as if you were reading "cppreference.com". They write things like template<auto...> struct C {}; and nobody argues. :-) Commented Mar 14, 2023 at 19:19
  • @JohnZwinck: Yes... Accessor<&Accessed::one_point> is nice. But how do I declare the template for Accessor? Commented Mar 14, 2023 at 19:22
  • They write things like template<auto...> struct C {}; and nobody argues. Sure, but their examples actually makes sense and are self-explained, even if they are a bit hard to wrap your head around sometimes. Asking you to clarify your question is not the same as arguing.
    – super
    Commented Mar 14, 2023 at 20:41

3 Answers 3

4

The class member type of passed class member pointer can be found by a trait. However, the syntax Accessor<decltype(&Accessed::one_point)> elimination, might require a deduction guide, which I leave it to you.

Assuming the Accessor would have an internal state for the member of the Accessed, you can round up a solution as follows:

#include <iostream>
#include <string>
#include <type_traits>

using Vector3d = std::string; // just for test

template <typename T> struct member {};
// traits for member type
template<typename Class, typename Type>
struct member<Type Class::*> {
    using mem_type = Type;
    using class_type = Class;
};
// alias types for convenience
template<typename T> using class_type_t = typename member<T>::class_type;
template<typename T> using mem_type_t = typename member<T>::mem_type;

template<typename T> class Accessor
{
    T mMember;
public:
    explicit Accessor(T mem)
        : mMember{mem}
    {}
    auto setMember(const Vector3d& point);  
};

class Accessed
{
    Vector3d one_point;
    Vector3d another_point;
public:

    struct OneAccessor : public Accessor<decltype(&Accessed::one_point)> {
        OneAccessor()
            : Accessor<decltype(&Accessed::one_point)>{ &Accessed::one_point }
        {}
        auto test(const Vector3d& point);
    };
    auto getOne() const { return one_point;  }
};

template<typename T>
auto Accessor<T>::setMember(const Vector3d& point)
{
    if constexpr (std::is_same_v<class_type_t<T>, Accessed>)
    {
        static class_type_t<T> obj{};
        obj.*mMember = point;
        return obj;
    }
}

auto Accessed::OneAccessor::test(const Vector3d& point)
{
    return this->setMember(point);
}

int main()
{
    Accessed::OneAccessor ob;
    auto var = ob.test("Hello world\n");
    std::cout << var.getOne();

    var = ob.test("Good bye\n");
    std::cout << var.getOne();
}

See a live demo in godbolt.org´


The following, leave it to the OP:

  • I have tweaked a bit of the original code such as Vector3d, getObject<T>(), etc.
  • There can be some more improvements done for the above, such as deduction guide or re-arrange some part of code, if valid.
2
  • I will finally find out what a "trait" is!!! <3 Commented Mar 14, 2023 at 19:29
  • Thank you for your time, dedication and patience! Yours is certainly the most complete and carefully written answer. I will accept @krisz's answer because it is simpler and more direct. I have learned a lot. :-) Commented Mar 15, 2023 at 1:16
2
template <typename T>
struct class_type;

template<typename T>
struct class_type<Vector3d T::*> {
    using type = T;
};

template<auto P>
struct Accessor
{
    using T = typename class_type<decltype(P)>::type;

    T& setMember(const Vector3d& point)
    {
        T& obj = getObject<T>();
        obj.*P = point;
        return obj;
    }
};

class Accessed
{
    Vector3d one_point;
    Vector3d another_point;

    class OneAccessor : public Accessor<&Accessed::one_point> {};
    class AnotherAccessor : public Accessor<&Accessed::another_point> {};
};

Another version using variables:

template<typename T>
struct Accessor
{
    Vector3d T::* p;

    constexpr Accessor(Vector3d T::* p) :p{p} {}

    T& setMember(const Vector3d& point)
    {
        T& obj = getObject<T>();
        obj.*p = point;
        return obj;
    }
};

struct Accessed
{
    Vector3d one_point;
    Vector3d another_point;

    static constexpr Accessor OneAccessor{&Accessed::one_point};
    static constexpr Accessor AnotherAccessor{&Accessed::another_point};
};
1
  • Okay... I didn't test it, yet... but I guess that if this works, it is what one is supposed to do whenever s/he attempts to template<typename T> template<... something that uses T>. :-) Commented Mar 14, 2023 at 21:06
2

I do not really understand your use case but to answer the question: it is possible to use a member function as a non-type parameter of a template. The following code illustrates the syntax:

#include <iostream>

class foo {
    int bar_;
public:
    void setBar(int n = 0) {
        bar_ = n;
    }
    int getBar() const {
        return bar_;
    }
};

template<typename T, void(T::*F)(int)>
class accessor {
    T& accessee_;
public:
    accessor(T& a) : accessee_(a) {};
    T& setMember(int n) {
        auto& obj = accessee_;
        (obj.*F)(n);
        return obj;
    }
};

int main()
{
    foo a_foo;
    accessor<foo, &foo::setBar> foo_accessor(a_foo);
    foo& ref_to_a_foo = foo_accessor.setMember(42);
    std::cout << ref_to_a_foo.getBar() << "\n";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.