Skip to content

Instantly share code, notes, and snippets.

@waquwex
Created July 14, 2024 18:31
Show Gist options
  • Select an option

  • Save waquwex/62d3aec8f6a24b427284ec0e246e4fbd to your computer and use it in GitHub Desktop.

Select an option

Save waquwex/62d3aec8f6a24b427284ec0e246e4fbd to your computer and use it in GitHub Desktop.
String literal and string as argument for templated function
// It needs to be converted into string to match this: const T& input
template <typename T>
typename enable_if<
is_same<typename decay<T>::type, string>::value || // type can decay into string
is_same<typename decay<T>::type, string_view>::value || // type can decay into string_view
is_convertible<T, const char*>::value>::type // const char* can be converted into the upper "same" types
printString(const T& input)
{
cout << "Printing string: " << input << endl;
}
// usage
string str = "C++ ++";
string_view str_vw(str);
str_vw.remove_prefix(2);
printString(str);
printString(str_vw);
printString("this is str literal!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment