Created
July 14, 2024 18:31
-
-
Save waquwex/62d3aec8f6a24b427284ec0e246e4fbd to your computer and use it in GitHub Desktop.
String literal and string as argument for templated function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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