I'm currently thinking about how best to restrict the general type of a template std::sting
, as well as string literals. Therefore, I am comparing the inferred type with the desired type using std::is_same
. In the case, std::string
it works right away. For a string literal meaning a char const array, it only works if I use std::decay
for a type, and then compare the result with the type char const *
. If I directly compare the inferred type with what, in my opinion, should be, is_same
returns false, as shown in the following code example.
template <class TYPE>
void function(TYPE&& parameter)
{
std::cout << typeid(TYPE).name() << " : " << typeid(char const [5]).name() << std::endl;
std::cout << std::is_same<char const [5], TYPE>::value << std::endl;
std::cout << typeid(std::decay_t<TYPE>).name() << " : " << typeid(char const *).name() << std::endl;
std::cout << std::is_same<char const *, std::decay_t<TYPE>>::value << std::endl;
}
int main(int argc, char** argv)
{
function("name");
return 0;
}
The generated output is as follows:
char const [5] : char const [5]
0
char const * __ptr64 : char const * __ptr64
1
Now I wonder why it is_same
returns false in the first case, even if the types seem to be the same.
, , , std::is_same
, std::decay
(, ). , , , .