Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
removed a tag
Link
Made the text shorter
Source Link

I would expect the last two lines of thisthe first code printingexample to print the same, i.e.

The types are deducted as I expect and the word "template"the overload resolution is also as I expect. ObviouslyHowever, if I explicitly type qualify the function call, then I get a different result then when the type deduction works correctly everywhereis deduced.

But if I type qualifyThe second code example repeats the called functionexercise replacing overload itresolution with specialization. In that case everything works in all cases except where the template argument is itself a templateas anyone would expect.

EDITCode Example 1: Added further overload with two type parameters to make things clearer for myself. Now it is clear to me that print<R, int>(r) has one single template argument and not two just as in template<template<class> class R, class T>. (1) and (2function template overloading) must either render the same or one of them must be an error. Anybody who thinks all compilers are doing correct would surely agree this is at least worth a compiler warning with -pedantic option.

When I replace function overloading withCode Example 2: (class template specialization, then template pattern matching works as I would have expected. See next code example. I have some trouble believing that the pattern matching rules also differ between classes and functions. If that is really true then I would say that Walter is right when he says, avoid it).

I would expect the last two lines of this code printing the same, i.e. the word "template". Obviously the type deduction works correctly everywhere.

But if I type qualify the called function overload it works in all cases except where the template argument is itself a template.

EDIT: Added further overload with two type parameters to make things clearer for myself. Now it is clear to me that print<R, int>(r) has one single template argument and not two just as in template<template<class> class R, class T>. (1) and (2) must either render the same or one of them must be an error. Anybody who thinks all compilers are doing correct would surely agree this is at least worth a compiler warning with -pedantic option.

When I replace function overloading with template specialization, then template pattern matching works as I would have expected. See next code example. I have some trouble believing that the pattern matching rules also differ between classes and functions. If that is really true then I would say that Walter is right when he says, avoid it.

I would expect the last two lines of the first code example to print the same.

The types are deducted as I expect and the the overload resolution is also as I expect. However, if I explicitly type qualify the function call, then I get a different result then when the type is deduced.

The second code example repeats the exercise replacing overload resolution with specialization. In that case everything works as anyone would expect.

Code Example 1: (function template overloading)

Code Example 2: (class template specialization).

narrowed the tag
Link
Added a further example where things work as I expect
Source Link
Loading
Compiler error is the best tag I think
Link
Loading
Added a fruther "two type" parameter overload to make things even clearer
Source Link
Loading
Added clang++ to the list of tested compilers
Source Link
Loading
just added some quotes to names in the comments
Source Link
Loading
Added Karthiks remark to the code
Source Link
Loading
just removed some newlines
Source Link
Loading
Source Link
Loading