1

I thought my problem was answered by this, but I can't get the following to compile:

#include <string>

template<class C>
class tKeySet {
  protected:
    bool set;
    static const std::string key;
};

template<class C, typename T>
class tKeySetType : private tKeySet<C> {
  protected:
    T m_val;
};

template<class C>
class tKeySetString: private tKeySetType<C, std::string> {
  public:
    tKeySetString<C>& operator=(const std::string &str) {
        this->set = true;
        this->m_val = str;
        return *this;
    }
};

class foo : private tKeySetString<foo> { };
template<> const std::string tKeySet<foo>::key = "foo key";

int main(void) {
    foo f;

    f = std::string("foo");

    return 0;
}

How can I make the assignment operator in tKeySetString<C> work with std::string?

Jamie
  • 7,075
  • 12
  • 56
  • 86

1 Answers1

1

foo privately inherits from tKeySetString<foo>, which means that operator= will not be part of its public interface.

You can bring it in by writing

public:
    using tKeySetString::operator=;

in the definition of foo.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • Changing all the inheritances to `public` _doesn't appear to work_ with what I've shown above, only the `using ...` portion of the answer works. Is there a way to avoid having to explicitly write `using ...` in all the instantiated templates? – Jamie Nov 27 '18 at 20:11
  • @Jamie: sorry, that was wrong. See https://stackoverflow.com/a/9161741/598696 for a reason. – Vittorio Romeo Nov 27 '18 at 20:24