Is it guaranteed that implicit type conversion is not considered in the type template argument?

Asked 1 years ago, Updated 1 years ago, 351 views

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.

C++11 Draft

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.

c++

2023-03-13 10:57

1 Answers

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


© 2024 OneMinuteCode. All rights reserved.