Hhut
Hhut

Reputation: 1198

Alias Declarations for template members

I'm trying to generalize runtime attribute getter/setters for my classes with compile time declarations. I thought I could do something like this:

// header
class arguments {
public:
  template <typename T, char const *NAME> void set_id(T const &id)
  {
    ///..
    set(NAME, id);
  }
};

class test : public arguments {
public:
  using set_alpha = arguments::set_id<double, "Alpha">;
};

// cpp
int main()
{
  test obj;
  obj.set_alpha(47.11);

  return 0;
}

Visual Studio 2019 is giving my some error C2061: syntax error: identifier 'set_id'. Is there a way to specify member functions like this with C++17 or what am I doing wrong here?

Thank you

Upvotes: 2

Views: 59

Answers (1)

Jarod42
Jarod42

Reputation: 217275

You cannot use using for that, you have to write the method:

class test : public arguments {
    static constexpr const char AlphaStr[] = {'A', 'l', 'p', 'h', 'a', '\0'};
public:
  auto set_alpha(double d) { return arguments::set_id<double, AlphaStr>(d); }
};

Demo

Upvotes: 4

Related Questions