chatlanin
chatlanin

Reputation: 61

ERROR: Cannot use parentheses when declaring variable with deduced class template specialization type

Good afternoon! Can someone explain why an error occurs when using a function where only one vector is passed?

Well, that is, there you can transfer constants without pain: 3, "asdf" 2.3, etc., but named variables are not. But for some reason a crossbreed is possible. Why is this suddenly possible?

#include <iostream>
#include <vector>

template<typename... Args>
struct func
{
  func(const Args&... args)
  {
    std::cout << "ok" << std::endl;
  }
};

template <typename... Args>
func(Args...) -> func<Args...>;

int main() 
{
  const std::vector<int> v { 1, 2, 3 };
  func(v);      // error is here
  func(v, 3);   // no error
  func("asdf"); // no error
}

ERROR: Cannot use parentheses when declaring variable with deduced class template specialization type

example: https://godbolt.org/z/jdnce5Gdv

Upvotes: 2

Views: 438

Answers (1)

molbdnilo
molbdnilo

Reputation: 66459

clang's error message is oddly specific – g++ and vc++ only complain about conflicting declarations of v– but note that it says "declaring a variable".

Since func is a type, and you can have parentheses around the identifier in a variable declaration, and because of the "if it can be interpreted as a declaration, it is a declaration" rule,

func(v);

is equivalent to

func v;

declaring a variable v of the type func.

Here is a shorter example of the same situation:

#include <vector>

int main() {
    int v = 0;
    std::vector<int>(v);
}

Upvotes: 2

Related Questions