Home > Back-end >  Declaring template functions with dependent types of arguments in C 20
Declaring template functions with dependent types of arguments in C 20

Time:09-17

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?

CodePudding user response:

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).

  • Related