Templates and wchar_t

I do not understand why I am getting the error "no instance of overload function "remove" matches the argument list. I am trying to pass a wchar_t and a const wchar_t. My project is set to use c++ 17 in VS2019. I am trying to follow along with a text reference (Modern C++ Cookbook). From what I have read, CharT should just be a placeholder to a string, wstring, etc. so I am not sure why I am getting this error.

Here is my templated function (directly from the text)
1
2
3
4
5
6
7
8
9
10
11
12
  template<typename CharT>
using tstring = std::basic_string<CharT, std::char_traits<CharT>, std::allocator<CharT>>;
template<typename CharT>
inline tstring<CharT> remove(tstring<CharT> text, CharT const ch)
{
	auto start = std::remove_if(std::begin(text), std::end(text), [=](CharT const c)
		{
			return c == ch;
		});
	text.erase(start, std::end(text));
	return text;
}


And then how I am trying to call it from main:
1
2
	std::wstring testString = L"This is a test string!";
        auto removeCharString{ remove(testString, L"!")};

auto removeCharString{ remove(testString, L'!')};

Putting "!" in quotes makes it a string, whereas putting it in single quotes makes it a character.

Also, it's a tough thing for VS2019 to handle (the compiler make choke). I had to use Clang to compile this to get error information (was getting a crash of cl.exe, typical of Microsoft).

The error was a bit cryptic, where this alternate formation made it easier in Microsoft

auto removeCharString = remove( testString, L'!');

Though I understand why you'd prefer the former - clang was better at reporting the nature of the problem.

If you aren't using clang, I can recommend (highly) that you modify the VS2019 install to use clang. I have to hand it to Microsoft, that in VS2019 and VS2017, clang installation and usage is about the best it can be on Windows.

It take a few tries to figure out how to set the project to use Clang. I personally create configurations of Debug/Release/ClangDebug/ClangRelease, setting the tool set for clang in the latter two.

It really does help a lot to use clang.

However, I don't suggest copying a configuration from a Microsoft configured configuration, then change it to clang. It gets confused, but fixes itself if you close and re-open the project. It seems that if you change toolsets on a existing project configuration (that you've changed any settings on), VS attempts to assign Microsoft compiler project settings to clang, which doesn't work (lots of complaints). Close and re-open the project seems to cause the IDE to re-evaluate and correct it.



Last edited on
Thanks, that did the trick. Will look into integrating clang into my setup. Appreciate the help and the quick response.
Topic archived. No new replies allowed.