0

Read about the user defined literals, and find it somehow obscure about defining a string literal overload from char[].

The definitely correct way is like this

std::string operator "" _s (const char* m, std::size_t)
{
    return std::string(m);
}

int main()
{
    std::string r = "hello,"_s + " world"_s;
    return 0;
}

But in fact the type of something like "hello" is const char (&)[N] (where N is the length). So I wonder if there is an alternative approach like this

template <std::size_t N>
std::string operator "" _s (const char (&m)[N])
{
    return std::string(m);
}

But oops the compiler (g++4.8 in Ubuntu 14.04) gave me an error

‘std::string operator"" _s(const char (&)[N])’ has invalid argument list
 std::string operator "" _s (char const (&m)[N])
                                               ^

Then I tried to remove the unused std::size_t parameter from the correct way (std::string operator "" _s (const char* m)) but got the error

unable to find string literal operator ‘operator"" _s’ with ‘const char [7]’, ‘long unsigned int’ arguments
     std::string r = "hello,"_s + " world"_s;

So is that mandatory to have the length as a parameter when the primary literal is a char[] or a wchar_t[]?

neuront
  • 9,312
  • 5
  • 42
  • 71
  • 1
    perhaps http://eel.is/c++draft/over.literal#3 applies – Piotr Skotnicki Jun 16 '16 at 09:24
  • 4
    Documentation: http://en.cppreference.com/w/cpp/language/user_literal#Literal_operators – Revolver_Ocelot Jun 16 '16 at 09:24
  • @Revolver_Ocelot Thanks. `float operator ""_e(const char*); // OK` Does it mean if the literal is `1111_e` it will match the overload without the length parameter? – neuront Jun 16 '16 at 09:33
  • @Revolver_Ocelot That's not "documentation" that's a public wiki. – uh oh somebody needs a pupper Jun 16 '16 at 09:34
  • 1
    @neuront String literals decay anyway, and there is no array constructor for `std::string`. So I'm unsure to what you're trying to do here. – uh oh somebody needs a pupper Jun 16 '16 at 09:36
  • @neuront Yes, it would if there are no `operator ""_e(unsigned long long )` defined. – Revolver_Ocelot Jun 16 '16 at 09:39
  • 2
    @uhohsomebodyneedsapupper it is a wiki aiming to document C++ language in readable and accessible form. Like expected _"Stackoverflow Documentation"_ – Revolver_Ocelot Jun 16 '16 at 09:41
  • @Revolver_Ocelot I know what the site is. I don't agree with people calling it documentation when it's a third party website that can be edited by anybody. Incorrect information has been posted there in the past. – uh oh somebody needs a pupper Jun 16 '16 at 09:43
  • 2
    @uhohsomebodyneedsapupper It is documentation inasmuch it documents something. Any documentation, whether equipped with some kind of official blessing or not, may contain errors (yes this includes standards of all kinds). – n. m. could be an AI Jun 16 '16 at 09:57
  • @n.m. The w3school has in much in common with the W3C as cppreference does with ISO C++. But so much as it's "documentation" it's just as good as the official thing, right? – uh oh somebody needs a pupper Jun 16 '16 at 10:00
  • 2
    @uhohsomebodyneedsapupper It could be as good as the official documentation. It could also be better or worse. Depends on how useful it is for a particular audience. cppreference.com is IMHO more useful than the actual standard for a common programmer, because a common programmer can derive answers he needs faster *and more accurately* on average from cppreference than from the standard. A compiler writer will probably find the standard more useful. – n. m. could be an AI Jun 16 '16 at 10:14
  • @n.m. You are missing the point. The point is people are carelessly referring to "cppreference" as **the** documentation as if it's authoritative. It's not. It's a public wiki that can be modified by **anybody** at **any time**. I'm not saying it's a bad site, I'm saying it's an issue of branding. Would you hire somebody who got a certificate from W3School because it's "good enough"? I hope not. – uh oh somebody needs a pupper Jun 16 '16 at 10:16
  • 2
    @uhohsomebodyneedsapupper I have no idea how good is W3School so let me refrain from discussing it. I do know that cppreference is very good indeed. I don't think many people mistake it for the authoritative reference. – n. m. could be an AI Jun 16 '16 at 10:31

0 Answers0