Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template argument deduction for 'char*'

Tags:

People also ask

What is template argument deduction in C++?

Class Template Argument Deduction (CTAD) is a C++17 Core Language feature that reduces code verbosity. C++17's Standard Library also supports CTAD, so after upgrading your toolset, you can take advantage of this new feature when using STL types like std::pair and std::vector.

Which parameter is legal for non-type template?

Which parameter is legal for non-type template? Explanation: The following are legal for non-type template parameters:integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.

How do I restrict a template type in C++?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.

What is C++ type deduction?

Type inference or deduction refers to the automatic detection of the data type of an expression in a programming language. It is a feature present in some strongly statically typed languages. In C++, the auto keyword(added in C++ 11) is used for automatic type deduction.


I have below function (Just for reproducing the issue):

template <typename KeyT>
void func(const KeyT cptr) {
  std::cout << typeid(KeyT).name() << std::endl;
}

I would like to call this with a string literal, like below:

func<char*>("literal");

But, I end up getting below warning:

warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wc++11-compat-deprecated-writable-strings]

I have a specific need to use char* as my Key type and I was expecting TAD to consider the param type as const char* in whole as I am not taking it by reference.

The warning comes with both clang and g++ compilers.

How is the param type being deduced here ?

Thanks in advance.