Hello.
I have written the following program and am curious why it prints "1" "2".
What are the exact effects of explicitly providing function template
parameters at the call? Is the second assign() function really a
specialization of the first assign() or is it an assign() overload?
Thank you.
-- Marek
-- cut --
#include <iostream>
class O { };
class M { };
template <class Left, class Right>
class Plus { };
template <class Left, class Right, class Op>
int assign( Left & left, const Right & right, const Op & op )
{
return 1;
}
template <class Op>
int assign( M & left, const Plus<M, M> & right, const Op & op )
{
return 2;
}
int main()
{
M m;
std::cout << assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ) <<
std::endl;
std::cout << assign( m, Plus<M, M>(), O() ) << std::endl;
}
-- cut -- 9 2743
Marek Vondrak wrote: Hello.
I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload?
Thank you. -- Marek
-- cut -- #include <iostream>
class O { }; class M { };
template <class Left, class Right> class Plus { };
template <class Left, class Right, class Op> int assign( Left & left, const Right & right, const Op & op ) { return 1; }
template <class Op> int assign( M & left, const Plus<M, M> & right, const Op & op ) { return 2; }
int main() { M m;
std::cout << assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ) << std::endl; std::cout << assign( m, Plus<M, M>(), O() ) << std::endl; } -- cut --
Based on reading the relevant section from Vandervoorde/Josuttis book,
the second assign function would be more specialized than the first
function and therefore, when explicity providing parameters, the second
one is selected.
<am******@gmail.com> píse v diskusním príspevku
news:11**********************@d71g2000cwd.googlegr oups.com... Marek Vondrak wrote: Hello.
I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload?
Thank you. -- Marek
-- cut -- #include <iostream>
class O { }; class M { };
template <class Left, class Right> class Plus { };
template <class Left, class Right, class Op> int assign( Left & left, const Right & right, const Op & op ) { return 1; }
template <class Op> int assign( M & left, const Plus<M, M> & right, const Op & op ) { return 2; }
int main() { M m;
std::cout << assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ) << std::endl; // [1] std::cout << assign( m, Plus<M, M>(), O() ) << std::endl; // [2] } -- cut --
Based on reading the relevant section from Vandervoorde/Josuttis book, the second assign function would be more specialized than the first function and therefore, when explicity providing parameters, the second one is selected.
This is what I would intuitively expect to happen but both G++ and MSVC
behave just opposite. If template parameters are specified explicitly, the
first assign function is called at [1] despite the fact that the second
assign function is more specialized. If template parameters are not
specified and are deduced by the compiler ([2]), the more specialized
function is called as expected.
Can anyone explain why is this happening?
-- Marek
> This is what I would intuitively expect to happen but both G++ and MSVC behave just opposite.
Sorry, I did not read the follow up properly. Correction: I would expect the
program to print "2" "2" (the specialized assign should be called in both
the cases, regardless of whether template parameters are specified or not).
-- Marek
Marek Vondrak wrote: Hello.
I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload?
Thank you. -- Marek
-- cut -- #include <iostream>
class O { }; class M { };
template <class Left, class Right> class Plus { };
template <class Left, class Right, class Op> int assign( Left & left, const Right & right, const Op & op ) { return 1; }
template <class Op> int assign( M & left, const Plus<M, M> & right, const Op & op ) { return 2; }
int main() { M m;
std::cout << assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ) <<
Well this surely cannot use the template with one parameter since you
are specifying three of them. That must print 1.
std::cout << assign( m, Plus<M, M>(), O() ) << std::endl;
This will prefer the template with one parameter. The compiler will
always select the most specialized template if it has a choice. This
must print 2.
}
Jonathan
Marek Vondrak wrote: Hello.
I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload?
Thank you. -- Marek
-- cut -- #include <iostream>
class O { }; class M { };
template <class Left, class Right> class Plus { };
template <class Left, class Right, class Op> int assign( Left & left, const Right & right, const Op & op ) { return 1; }
template <class Op> int assign( M & left, const Plus<M, M> & right, const Op & op ) { return 2; }
int main() { M m;
std::cout << assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ) << std::endl; std::cout << assign( m, Plus<M, M>(), O() ) << std::endl; }
assign function templates are disparate function templates. There is no
partial template specialization for functions. The first function
template has three template arguments and the second has one. Therefore
the second function template can not be used with three template
arguments specified as you do.
>> Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload?
Thank you. -- Marek
-- cut -- #include <iostream>
class O { }; class M { };
template <class Left, class Right> class Plus { };
template <class Left, class Right, class Op> int assign( Left & left, const Right & right, const Op & op ) { return 1; }
template <class Op> int assign( M & left, const Plus<M, M> & right, const Op & op ) { return 2; }
int main() { M m;
std::cout << assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ) << std::endl; std::cout << assign( m, Plus<M, M>(), O() ) << std::endl; } assign function templates are disparate function templates. There is no partial template specialization for functions.
Really? I think this was true in the prestandard era. Since C++98 there is a
partial specialization of functions and partional ordering rules.
The first function template has three template arguments and the second has one. Therefore the second function template can not be used with three template arguments specified as you do.
Although I see the rationale behind your explanation, please consider the
following program.
class O { };
class M { };
template <class Left, class Right>
class Plus { };
// AS1
template <class Left, class Right, class Op>
void assign( Left & left, const Right & right, const Op & op ) { } //
Primary template
// AS2
template <>
void assign( M & left, const Plus<M, M> & right, const O & op ) { } //
Explicit specialization of AS1
int main()
{
M m;
assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ); // Calls AS2
}
Why does it call AS2? It occurs to me, that AS1 is treated as the primary
template and AS2 as its explicit specialization. In the same sense I would
then believe that in the original program the first assign is the primary
template and the second assign its partial specialization:
template <class Left, class Right>
class Plus { };
// AS1
template <class Left, class Right, class Op>
void assign( Left & left, const Right & right, const Op & op ) { } //
Primary template again.
// AS2
template <class Op>
void assign( M & left, const Plus<M, M> & right, const Op & op ) { } //
Should be partial specialization of AS1?
int main()
{
M m;
assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ); // Calls AS1! Why?
This is inconsistent with the explicit specialization example!
}
-- Marek
Marek Vondrak wrote: Hello.
I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload?
Thank you. -- Marek
-- cut -- #include <iostream>
class O { }; class M { };
template <class Left, class Right> class Plus { };
template <class Left, class Right, class Op> int assign( Left & left, const Right & right, const Op & op ) { return 1; }
template <class Op> int assign( M & left, const Plus<M, M> & right, const Op & op ) { return 2; }
int main() { M m;
std::cout << assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ) << std::endl; std::cout << assign( m, Plus<M, M>(), O() ) << std::endl; } assign function templates are disparate function templates. There is no partial template specialization for functions.
Really? I think this was true in the prestandard era. Since C++98 there is a partial specialization of functions and partional ordering rules.
I repeat. There is no *partial* specialization for function templates. The first function template has three template arguments and the second has one. Therefore the second function template can not be used with three template arguments specified as you do.
Although I see the rationale behind your explanation, please consider the following program.
class O { }; class M { };
template <class Left, class Right> class Plus { };
// AS1 template <class Left, class Right, class Op> void assign( Left & left, const Right & right, const Op & op ) { } // Primary template
// AS2 template <> void assign( M & left, const Plus<M, M> & right, const O & op ) { } // Explicit specialization of AS1
int main() { M m;
assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ); // Calls AS2 }
Why does it call AS2? It occurs to me, that AS1 is treated as the primary template and AS2 as its explicit specialization.
AS2 is a *full* function template specialization because it has no
template arguments.
In the same sense I would then believe that in the original program the first assign is the primary template and the second assign its partial specialization:
Your belief is premised on a false assumption that there exists partial
function template specialization.
> I repeat. There is no *partial* specialization for function templates. Your belief is premised on a false assumption that there exists partial function template specialization.
Okay, thanks Maxim and sorry for my doubts. I thought partial specialization
for function templates was supported. That is why I originally asked whether
the second assign() was actually a *specialization* or an *overload* of the
first assign. I also asked in comp.lang.c++.moderated and was luckily
provided with links to relevant articles. Now, it should be clear. Thanks
for help.
-- Marek Hello.
I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload?
Thank you. -- Marek
-- cut -- #include <iostream>
class O { }; class M { };
template <class Left, class Right> class Plus { };
template <class Left, class Right, class Op> int assign( Left & left, const Right & right, const Op & op ) { return 1; }
template <class Op> int assign( M & left, const Plus<M, M> & right, const Op & op ) { return 2; }
int main() { M m;
std::cout << assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ) << std::endl; std::cout << assign( m, Plus<M, M>(), O() ) << std::endl; } assign function templates are disparate function templates. There is no partial template specialization for functions.
Really? I think this was true in the prestandard era. Since C++98 there is a
partial specialization of functions and partional ordering rules.
The first function template has three template arguments and the second has one. Therefore the second function template can not be used with three template arguments specified as you do.
Although I see the rationale behind your explanation, please consider the
following program.
class O { };
class M { };
template <class Left, class Right>
class Plus { };
// AS1
template <class Left, class Right, class Op>
void assign( Left & left, const Right & right, const Op & op ) { } //
Primary template
// AS2
template <>
void assign( M & left, const Plus<M, M> & right, const O & op ) { } //
Explicit specialization of AS1
int main()
{
M m;
assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ); // Calls AS2
}
Why does it call AS2? It occurs to me, that AS1 is treated as the primary
template and AS2 as its explicit specialization. In the same sense I would
then believe that in the original program the first assign is the primary
template and the second assign its partial specialization:
template <class Left, class Right>
class Plus { };
// AS1
template <class Left, class Right, class Op>
void assign( Left & left, const Right & right, const Op & op ) { } //
Primary template again.
// AS2
template <class Op>
void assign( M & left, const Plus<M, M> & right, const Op & op ) { } //
Should be partial specialization of AS1?
int main()
{
M m;
assign<M, Plus<M, M>, O>( m, Plus<M, M>(), O() ); // Calls AS1! Why?
This is inconsistent with the explicit specialization example!
}
-- Marek
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: claude uq |
last post by:
Hello,
Am trying to use template functions within some class to convert int, float,
doubles, etc into strings.
Below, three ways to do it via use of "to_string(const T & Value)" . The
only...
|
by: Frank Schmitt |
last post by:
Hi!
Suppose I have a class template Foo
with a member function template bar - so far so good:
template <typename T>
struct Foo {
template <typename U>
void bar(const U& u);
};
|
by: richardclay09 |
last post by:
Hi
Can someone please write some "compare and contrast" notes for
"Template functions vs. function objects"? When to use one and not the
other? For example, the TF square_f does the same thing as...
|
by: REH |
last post by:
I need to create template functions with the same name but different number
of template parameters. My compiler says this is illegal:
template<class TO, class F1>
TO convert_to();
...
|
by: Andrei Tarassov |
last post by:
Hi!
I would like to know if it is possible to implement something like
this:
template <class X, class Y>
X void func(X, Y) { ... }
template <class X, class Y>
Y void func(X, Y) { ... }
| |
by: shuisheng |
last post by:
Dear All,
I am confused about the template functions in template class. Such as I
have
template class A <class T, int n>
In side the class, I have
function A<T,nfun() and
|
by: newbie |
last post by:
I happened to read boost library code and realized that most (the part
I read) functions are inlined like:
template <class Key>
inline void Foo(const Key& k) {
...
...
}
Is there a strong...
|
by: vectorizor |
last post by:
Hello all,
I am attempting to vectorize few template functions with the Intel
compiler, but without much success so far. Ok granted, this question
is not 100% c++, but it is related enough that...
|
by: Olaf |
last post by:
Hi,
I'm writing a small wrapper for libcurl. For Options I use:
template<typename T, long CURLOPT_ID>
class CurlOption: boost::noncopyable
{
public:
typedef typename...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |