Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please see C++ 20 code below . I am merely attempting to accept a basic_string<chartype> as an argument to function foo . If an L"..." type is attempted compile error results . However it is possible to construct a local wstring type variable from an L"..." type . Why is this not a contradiction . If the local variable can be so consructed why not the argument to the function foo . The type of "..." as indicated by typeid().name() is charType[number of characters] which I assume is an array which I do not have much knowledge or experience of . The basic_string constructor documentation on cppreference.com does not show arrays as being supported so I do not understand why the local variable should be constructed from this type . The documentation refers to something called a StringViewLike however i have no experience w/ this type however the documentation of its constructor also makes no mention of array type . Thank You Kindly
C++
#include <string>
#include <iostream>
using namespace std;

template<typename charType>
void foo(basic_string<charType> _basic_string);

int main()
{
	wstring _wstring_a(L"aaa");  // okeedokee
	wstring _wstring_b = L"bbb"; // okeedokee

	foo(L"abc"); // error below
#ifdef _UNDEFINED_
	error C2672 : 'foo' : no matching overloaded function found
		message : could be 'void foo(std::basic_string<_Elem,std::char_traits<_Elem>,std::allocator<_Ty>>)'
		message : 'void foo(std::basic_string<_Elem,std::char_traits<_Elem>,std::allocator<_Ty>>)' : could not deduce template argument for 'std::basic_string<_Elem,std::char_traits<_Elem>,std::allocator<_Ty>>' from 'const wchar_t [4]'
#endif
		return 0;
}
I can of course pass all such arguments as e.g. foo(wstring(L"text goes here")) but i do not beleive i should have to invoke wstring explicitly .

What I have tried:

Read basic_string constructor documentation on cppreference.com
Posted
Updated 18-Jan-23 3:37am

A workaround:
C++
#include <iostream>
using namespace std;

template<typename charType>
void foo(basic_string<charType> _basic_string)
{
  std::cout << "string\n";
}

template <typename charType>
void foo(const charType * pct)
{
  std::cout << "pointer, ";
  foo( basic_string<charType>{pct});
}


int main()
{
  foo(wstring{L"Hello"});
  foo(L"World!");
}
 
Share this answer
 
Comments
BernardIE5317 18-Jan-23 9:47am    
Thanks As long as i do not have to explicitly construct basic_string<> at each invocation i am reasonably happy though i still do not understand why compiler permits local construction but not upon pass to function .
You can do this
C++
wstring _wstring_a(L"aaa");  // okeedokee
	wstring _wstring_b = L"bbb"; // okeedokee


because those are runtime conversion operations. When you compile this, the compiler understands what you are trying to do and inserts the appropriate conversions to 'make' a wstring. The difference is that L"bbb" is not a wstring, but the compiler knows how it can make a wstring at runtime.

What you are trying to do is at compile time providing a type that the compiler wants to use to determine chartype. But because it is not a basic_string, it doesn't know how to do it.

If you do this: foo(wstring(L"text goes here"))
Then the compiler knows which template argument to use because you're explicitly telling it.
The solution is either to do this, or help the compiler find its way to the correct solution

The best I can come up with atm is this
C++
template<typename chartype >
void foo(basic_string<chartype> _basic_string)
{
    //...
}

template<typename T>
void foo(T* ptrstr)
{
    foo(basic_string<std::decay_t<t>>(ptrstr));
}


Which basically takes a pointer that is pointing to a type that can be used as a basic_string character type. It will then remove the 'const' from the type because the standard doesn't allow it, and use that datatype as template argument for basic_string
 
Share this answer
 
v5
Comments
BernardIE5317 18-Jan-23 9:48am    
Thanks for the assistance It seems similar to CPallini .
Bruno van Dooren 18-Jan-23 9:57am    
It is very similar. It's a different way of approaching the same solution, with an explanation of why your original approach doesn't work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900