Is it guaranteed that the primary template will be selected when a class template with full specialization of long
is materialized with int
, as shown in the following code?
#include<iostream>
template<typename T>
structure A
{
A( )
{
std::cout<<"primary"<<std::endl;
}
};
template<>
structure A<long>
{
A( )
{
std::cout<<"specialized"<<std::endl;
}
};
int main()
{
A<int>b2;
}
output
primary
I've been implementing it in the hope that the output will look like the one above, but when I thought about it carefully, I didn't know if this was a defined behavior.
14.7 Template installation and specialization
or
Template-Related
for cppreference.com
I read as much as I could, but I did my best!
Thank you for your cooperation.
Quotations are acceptable, and articles, chapter numbers, etc. JIS X3014:2003
Perhaps you are confused that you can call func(long)
(where the argument type is int
instead of long
) especially for the general integer promotion 4.5
, but implicitly for this integer promotion
In the case of
template
type resolution, the candidate function selection logic per 13.3.1
, 13.3.2
and 13.3.3
is considered as a priority over the general integer promotion.
#include<iostream>
template <typename T> structure A {
A(T) {std::cout<<"primary"<<std::endl;}
};
template<>struct A<long>{
A(long) {std::cout<<"long"<<std::endl;}
};
template<>struct A<short>{
A(short) {std::cout<<"short"<<std::endl;}
};
template<>struct A<char>{
A(char) {std::cout<<"char"<<std::endl;}
};
template<>struct A<int>{
A(int) {std::cout<<"int"<<std::endl;}
};
int main()
{
Aa1(0);
Aa2('\0');
Aa3(0L);
Aa4(0U);
}
Try commenting on some of the specializations or specifying the values of the arguments in suffixes to see how they work.
2023-03-13 13:43
If you have any answers or tips
Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656
© 2025 OneMinuteCode. All rights reserved.