Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

In the series "crashing Visual C++ 2015", today's example

34 views
Skip to first unread message

Alf P. Steinbach

unread,
Jan 28, 2016, 1:24:43 PM1/28/16
to
Not sure whether this code is formally correct (it doesn't compile with
g++):


<code>
#include <stdio.h>

template< class... Args >
auto format_for( Args... args, int last_arg )
-> char const*
{
// using Specifier = char const [3];
// static Specifier const s[] = { {'%', 'f'+(0*args), ' '}...,
{'%', 'f', 0} };
return reinterpret_cast<char const*>( "" );
}

auto main() -> int
{
printf( "'%s'\n", format_for( 5, 2, 1 ) );
}
</code>


Anyway it crashes Visual C++ 2015 update 1:


<example>
C:\my\forums\so\081> cl printf.cpp /Feb
printf.cpp
printf.cpp(14): fatal error C1001: An internal error has occurred in the
compiler.
(compiler file 'f:\dd\vctools\compiler\cxxfe\sl\p1\cxx\dymto.c', line 6771)
To work around this problem, try simplifying or changing the program
near the locations listed above.
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information

C:\my\forums\so\081> g++ printf.cpp
printf.cpp: In function 'int main()':
printf.cpp:14:43: error: no matching function for call to
'format_for(int, int, int)'
printf( "'%s'\n", format_for( 5, 2, 1 ) );
^
printf.cpp:4:6: note: candidate: template<class ... Args> const char*
format_for(Args ..., int)
auto format_for( Args... args, int last_arg )
^
printf.cpp:4:6: note: template argument deduction/substitution failed:
printf.cpp:14:43: note: candidate expects 1 argument, 3 provided
printf( "'%s'\n", format_for( 5, 2, 1 ) );
^
</example>


I've posted an SO question about it, and I will probably post a bug
report to Microsoft Connect if or when I learn what to expect as correct
behavior, or after waiting some resonable time for that.


Cheers,

- Alf

Ian Collins

unread,
Jan 28, 2016, 2:10:54 PM1/28/16
to
Alf P. Steinbach wrote:
> Not sure whether this code is formally correct (it doesn't compile with
> g++):
>
>
> <code>
> #include <stdio.h>
>
> template< class... Args >
> auto format_for( Args... args, int last_arg )
> -> char const*

I thought the parameter pack had to be the last parameter?

--
Ian Collins

Öö Tiib

unread,
Jan 28, 2016, 2:31:32 PM1/28/16
to
Yes, if you want it to be deduced by compiler (that Alph's code attempts).

[temp.deduct.type]/p5:
The non-deduced contexts are:
[...]
* A function parameter pack that does not occur at the end of the parameter-declaration-list.

Alf P. Steinbach

unread,
Jan 28, 2016, 2:45:58 PM1/28/16
to
Yes, thanks.

That's also what I learned on SO now.

Funny, I've been using variadic templates but evidently, by chance,
always with the parameter pack at the end, or explicitly specified.


Cheers!,

- Alf

0 new messages