2

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

Hhut
  • 1,128
  • 1
  • 12
  • 24
  • 1
    Does this answer your question? [Passing const char\* as template argument](https://stackoverflow.com/questions/3773378/passing-const-char-as-template-argument) – anatolyg Mar 02 '21 at 08:16
  • Thank you, but the const char* argument is only one part, in fact I'm struggeling more with the member function using declaration; since the problem is the same without a string template argument. – Hhut Mar 02 '21 at 08:34

1 Answers1

4

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

Jarod42
  • 203,559
  • 14
  • 181
  • 302