Home Use proper string literal in templated function in C++
Reply: 0

Use proper string literal in templated function in C++

user8916
1#
user8916 Published in September 21, 2018, 8:05 am

I have a function, that is templated to match every std::basic_string instantiation:

template <typename _valueType>
void addFoo(std::basic_string<_valueType>& string_)
{
    string_ += "foo";
}

I want "foo" to be _valueType-dependent literal. I would like not to use specialization, because in the actual project I have whole class templated and it would be a lot of work.
I am currently using if constexpr in function body, but problems start, when I have function taking default parameter like this:

template <typename _valueType>
void foo(_valueType const delimiter_ = '.');
share|improve this question
  • 2
    There are an unbounded number of basic_string instantiations that could exist. You are also missing a template argument for basic_string -- it takes 2 not 1 type (the second is a traits class): I could write a basic_string<double, SomeTraits>. In addition, one of yours cases use a '.' (single quote), the other a string "foo" double quote. Please be less vague, what does your actual problem entail? – Yakk - Adam Nevraumont Feb 13 at 17:01
  • I decided to support only the default traits, but if there is strong argument against this method, please inform me. My actual problem is described in the question: I want to use proper string/character literal depending on which string instantiation takes place. – Poeta Kodu Feb 13 at 17:09
  • For some reason you ignored my first sentence. I'll make it longer: basic_string<char16_t>, basic_string<char> basic_string<unsigned char> basic_string<char32_t> basic_string<wchar_t> are all possible basic_string types. I have no idea what _valueType can vary over; while I expect you do. If there is no limit on what it can vary over, there are different good answers than if (say) you support exactly 2. – Yakk - Adam Nevraumont Feb 13 at 18:10
  • I will limit it to the types you mentioned using SFINAE, because I want it to support all these. Thanks. – Poeta Kodu Feb 13 at 18:44

2 Answers 2

active oldest votes
up vote 2 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.370182 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO