r/cpp • • Dec 08 '18

A better C++ dice roller class

https://github.com/CaptainCrowbar/dice
1 Upvotes

17 comments sorted by

View all comments

1

u/LKummer Dec 08 '18 edited Dec 08 '18

Isn't the use of string_view causing undefined behavior? For example in the example code:

auto dice2 = Dice("2d10x5 + 3d6 + 10");

Since the string passed is a rvalue temporary, it's getting destructed before the string_view is accessed. So it's accessing a dangling pointer?

Same with line 10 in Dice.cpp? (I also think the static here doesn't actually do anything since it's getting destructed when the lambda finishes executing? Am I wrong? )

static constexpr std::string_view whitespace = "\t\n\f\r ";

I'd love if someone with more experience than me would pitch it, I usually avoid string_view because of these dangling "issues", only using it in later stages as an optimization.

Sneaky edit: I was wrong, this is not undefined behavior, thanks for /u/Wurstinator for pointing it out.

Since you're using MSVC I'd also replace strtoll to from_chars just for the hell of it. this blog post was posted here recently, which introduced me to it and it looks pretty neat.

5

u/Wurstinator Dec 08 '18

0

u/LKummer Dec 08 '18 edited Dec 08 '18

You're right, I got the terminology wrong, was thinking temporary in my head.

Aren't they still temporary though? Getting destructed before the string_view is accessed?

1

u/Wurstinator Dec 08 '18

A string literal is a pointer, so it's the same as passing e.g. the literal 1 to a function. The constructor takes that pointer by value, so it's simply copied and remains alive for the call of the function.

13

u/STL MSVC STL Dev Dec 08 '18

String literals aren't pointers - they are arrays that can decay to pointers.

1

u/Wurstinator Dec 08 '18

What is the difference in semantics?

6

u/STL MSVC STL Dev Dec 08 '18

sizeof and decltype can observe the difference. A reference to an array can bind to a string literal, but it can't bind to a pointer.

5

u/Quincunx271 Author of P2404/P2405 Dec 08 '18

If you pass one to an overload set where one function takes a const char* and the other takes a const char (&)[N], the latter will be selected

5

u/STL MSVC STL Dev Dec 08 '18

That is not true due to the arcane details of how overload resolution works. However, it is possible to observe if you carefully craft your overload set:

C:\Temp\gcc>type meow.cpp
#include <stdio.h>

void meow(const char* ptr) {
    printf("meow(const char* ptr) %s\n", ptr);
}
template <size_t N> void meow(const char (& arr)[N]) {
    printf("meow(const char (& arr)[N]) %s\n", arr);
}

void hiss(const char* const& ptr) {
    printf("hiss(const char* const& ptr) %s\n", ptr);
}
template <size_t N> void hiss(const char (& arr)[N]) {
    printf("hiss(const char (& arr)[N]) %s\n", arr);
}

template <typename CharT> void purr(const CharT* const& ptr) {
    printf("purr(const CharT* const& ptr) %s\n", ptr);
}
template <size_t N> void purr(const char (& arr)[N]) {
    printf("purr(const char (& arr)[N]) %s\n", arr);
}

int main() {
    const char * p = "pointer";
    meow(p);
    meow("string literal");
    hiss(p);
    hiss("string literal");
    purr(p);
    purr("string literal");
}

C:\Temp\gcc>g++ -Wall -Wextra meow.cpp -o meow.exe && meow
meow(const char* ptr) pointer
meow(const char* ptr) string literal
hiss(const char* const& ptr) pointer
hiss(const char* const& ptr) string literal
purr(const CharT* const& ptr) pointer
purr(const char (& arr)[N]) string literal

2

u/Quincunx271 Author of P2404/P2405 Dec 08 '18

Is this special for string literals, or is it true for any array? I completely expected meow("string literal") to output meow(const char (& arr)[N]) string literal.

3

u/STL MSVC STL Dev Dec 09 '18

It’s true for any array. The issue is that overloading values and references can behave unintuitively, even if you know about decay. The hiss() example shows that the template tiebreaker is even involved. This is one of the reasons why I say that overload resolution needs to be treated with care - it works well if you know how to avoid creating complicated scenarios.

2

u/LKummer Dec 08 '18 edited Dec 08 '18

Oh! You're absolutely right. It's also mentioned in the notes on the cppreference page. I didn't realize the lifetime of string literals worked like this (though it makes perfect sense with them being char arrays), thank you for pointing out my mistake and teaching me something.

I'd still argue against using string_view in both cases, safety in the constructor and short string optimization in both. Though these arguments are subjective and you're completely correct about it not being undefined behavior, so the use of string_view here is not wrong.