0

I have a piece of code resembling the following (other details omitted for brevity):

template <uint First, uint Second, typename T>
struct Thing {
  std::shared_ptr<T> asdf;
  uint get() const { this->asdf->get_value<First,Second>(); }
};

... producing the following error in GCC 7:

error: expected primary-expression before ')' token
  uint get() const { this->asdf->get_value<First,Second>(); }
                                                         ^

Clang 4.0.1 reports no warnings with -Weverything (excepting very pedantic things like c++98 compatibility warnings); same thing with GCC 7 and -W{all,extra,effc++,pedantic}.


I was previously unaware of this question; while the answer to my problem is certainly outlined there, the error message didn't make it plain that was the problem. Supposing I deleted this question and its associated answer, other people searching for this error message will only run across the questions I found -- that is, the ones indicating the problem was caused by passing a type as a function argument instead of passing a value.

Brian Vandenberg
  • 4,011
  • 2
  • 37
  • 53
  • @Rakete1111 According to [this blog](https://stackoverflow.blog/2011/01/05/the-wikipedia-of-long-tail-programming-questions/), I don't think this is a duplicate. – xskxzr Apr 26 '18 at 04:23
  • @xskxzr Could you expend a bit please? Because the OP needed to put a `template` in there, which is what the dupe answers by explaining why it's needed. – Rakete1111 Apr 26 '18 at 04:36
  • @Rakete1111 They are two *different* questions with the same answer. I found a discussion [here](https://meta.stackexchange.com/questions/74080/close-as-duplicate-what-if-only-the-answer-is-a-duplicate). To be honest, I am not very sure whether it is a duplicate. – xskxzr Apr 26 '18 at 05:00

1 Answers1

4

TLDR: The solution was to use the template keyword as a disambiguator (at the bottom of the page) when calling get_value<A,B>():

uint get() const { auto tmp = this->asdf->template get_value<First,Second>(); return tmp; }
                                          ^^^^^^^^^

The other answers I came across related to this error were a result of the code passing a type instead of a value as an argument to the function.

In this case the function takes no arguments. What made the problem obvious was changing the form of the code, producing better errors:

template <uint First, uint Second, typename T>
struct Thing {
  std::shared_ptr<T> asdf;
  uint get() const { auto tmp = this->asdf->get_value<First,Second>(); return tmp; }

... which changed the errors to:

error: declaration of 'Second' shadows template parameter
  uint get() const { auto tmp = this->asdf->get_value<First,Second>(); return tmp; }

error: declaration of 'Second' with type 'auto' requires an initializer
  uint get() const { auto tmp = this->asdf->get_value<First,Second>(); return tmp; }
                                                            ^
error: expected ';' at end of declaration    };
  uint get() const { auto tmp = this->asdf->get_value<First,Second>(); return tmp; }
                                                                  ^
Brian Vandenberg
  • 4,011
  • 2
  • 37
  • 53