c++ - Template argument deduction of string literal -


consider simple function

template<typename t> void func(const t& x) {std::cout<< typeid(t).name();} 

now if call function func("ddd") , t deduces to? . if there no const in func's parameter , t char [4] , whats confusing me addition of const , t deduces ?

is : const char [4] . if change parameter t const &x (i.e change order of const) deduction produces t char const [4] ?

can explain argument deduction string literals?

string literals arrays of const characters.

a reference string literal of 4 chars of type char const (&)[4].

const char [4] , char const [4] same types!

char const (&)[n], const char [n] , char const [n] deduce char const [n]

#include <iostream>  template<typename t> void func1(t& x) {std::cout<< typeid(t).name()<<std::endl;}  template<typename t> void func2(const t& x) {std::cout<< typeid(t).name()<<std::endl;}  template<typename t> void func3(t const &x) {std::cout<< typeid(t).name()<<std::endl;}  int main() {     char c[4]= {'a','b','c','d'};     const char c1[4]= {'a','b','c','d'};     char const c2[4]= {'a','b','c','d'};      func1("abcd"); //prints char const [4]     func1(c); //prints char [4]     func1(c1); //prints char const [4]     func1(c2); //prints char const [4]      func2("abcd"); //prints char const [4]     func2(c); //prints char [4]     func2(c1); //prints char const [4]     func2(c2); //prints char const [4]      func3("abcd"); //prints char const [4]     func3(c); //prints char [4]     func3(c1); //prints char const [4]     func3(c2); //prints char const [4]      return 0; } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -