c++ - basic_string<CharT> vs. CharT* -


This is a common question, yet I could not find a satisfactory answer. In my project we support std :: string and still have to support broad strings. So we want to go to basic_string , but then, things stop working well and the parameters should be explicitly excluded:

  # Include & lt; String & gt; Template & lt; Typename chart, typename attributes, typed anonymous allocation & gt; Void foo (const std :: basic_string & lt; CharT, symptom, allocation & gt; & amp;) {} template void foo (const std :: string & amp;;); // templates void // foo & lt; Char, std :: char_traits & lt; Char & gt ;, std :: olocator & lt; Char & gt; & Gt; (Const std :: string & amp;;); Zero times (const std :: string and) {} ​​int main () {bar ("abc"); Foo & lt; Char, std :: char_traits & lt; Char & gt ;, std :: allocator & lt; Char & gt; & Gt; ("Def"); Foo ("def"); }  

OK, it fails for famous reasons:

  clang ++ - mp-3.5 -wall -std = c ++11 foo .cc Foo.cc:20:3: Error: Function to call 'Foo' ("DIF") does not match any functions; ^ ~ ~ Foo.cc: → Note: The candidate template was ignored: 'basic_string & lt; Type-parameter-0-0, type-parameter-0-1, type-parameter-0-2 & gt; Against 'Four Constants [4]' Fu (Const Std :: basic_string & lt; Chart, Symptoms, Allocation & gt; & amp; nbsp;):  

What I do not get, code > Bar ? Why is char (with either explicit template parameters or deductions) for foo not enough to solve this problem?

It seems that it means that instead of using templates and basic_string in the highlighted APIs, we have to use it as an implementation description, but the user std :: string

thanks the bar ("ABC") for four consoles [4] There may be an indirect conversion of std :: string to "itemprop =" text ">

. foo in it from bar It's not actually a function, but a The function template is required to know its template arguments in order to create the correct function.

The first column explicitly provides the template logic for foo Therefore, it creates a function that looks like this: Void foo (const std :: basic_string & lt; char, std :: char_traits & lt; char & gt ;, std :: allocator & lt; char & gt; & Amp;;;);

Kick in the underlying conversion and everything is fine.

The third call does not provide template logic, The link to CharT , symptoms and recurrent by type four consents [4] . This type of information does not take along with it, so the deduction failed and overloaded resolution can not get the right function.


Comments