1

In C++20, a template function can be declared in a simplified way using auto keyword and omitting template<class T> prefix. But if the second/third/… argument type of a template function depends on the first template argument type, is the declaration with auto equivalent?

Consider an old-style template example:

template<typename T>
void f(T x, std::optional<T> y, std::array<char,sizeof(T)> z);

The same template function declaration with auto-syntax will be:

void g(auto x, std::optional<decltype(x)> y, std::array<char,sizeof(x)> z);

Unfortunately, Visual Studio 2019 16.11.2 rejects the second variant with the errors:

error C3539: a template-argument cannot be a type that contains 'auto'
error C3540: sizeof cannot be applied to a type that contains 'auto'

Demo: https://gcc.godbolt.org/z/vc6bE14jh

Is it just a limitation/bug of Visual Studio?

1
  • @JeJo: That’s about template parameters, not function parameters. Commented Sep 11, 2021 at 7:01

1 Answer 1

3

Yes, this is an MSVC bug, but g and f are not the same here because g deduces only from its first argument (which may or may not be what you want).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.