#include <string>
#include <type_traits>
#include <cuchar>
template<typename StringType>
requires std::is_same_v<typename StringType::value_type, char16_t>
using Function = std::c16rtomb;
template<typename StringType>
requires std::is_same_v<typename StringType::value_type, char32_t>
using Function = std::c32rtomb;
template<typename StringType>
std::string SomeFunction(const StringType& param)
{
// Expected use:
// Conditionally use different function
Function<StringType>(/* args */);
}
// Expected use:
std::string32 test U"test";
SomeFunction(test);
I have never been good with templates as you can see.
What I'm trying to achieve is to use different function depending on what kind of a string is used.
Interesting example I like it, the only problem is that conversion error should not imply skipping characters because this would result in truncated string, in other words it would be data loss.
For example if you read data from file and do conversion followed by writing converted data back to file then either entire data is converted or no conversion is made.
Therefore to_mb function should either throw or the design should be changed so that the result is an empty string.