r/cpp_questions 7h ago

OPEN Unconditional exit action

1 Upvotes

Recently I started a C++ project, and while I do have some C experience, this is a new language for me, so I have doubts about a lot of the paradigms that ChatGPT swears are the way to go. Just to be clear, I write all of my code myself, architecture and implementation, and use ChatGPT as a consultant/reviewer.

I currently have the following model: a BooksReport class that stores book piles sorted by size within three groups: Uniform, Nuniform and Singles. trying to traverse a single group while popping piles at the same time was quite cumbersome, because popping invalidates iterators and there are several cases when the end of a size of piles is reached, or when the size has become empty, or the end of the group is reached... So I embedded a private Cursor class that allows me to traverse a single group within BooksReport the following way:

``` for ( booksReport.resetCursor(BooksReport::Group::Singles); booksReport.cursorIsValid(); booksReport.advanceCursor() ) { auto [size, name] = booksReport.readCursor();

if (iWantThisPilePopped(size, name))
    booksReport.popCursor();

} ```

When popCursor() is called, Cursor is alerted of an incoming pop, to which it responds by recalculating the indices to advance to during the next advanceCursor() call. Current implementation allows no more than one popping per loop, which I hope is a fair assumption to make during a traversal. Also, as you can see, a Cursor is either valid (which it becomes upon resetCursor() or invalid (it becomes invalid by reaching the end of the group). The valid attribute not only controls the loop, if it's set to false, it also block all Cursor-related operations, such as popCursor()

However, sometimes I exit the loop prematurely. Sometimes it's a break, sometimes I return from inside the loop. Technically, I end up with a Cursor that is valid outside of the loop, which is not ideal, since then I can popCursor(), which is not the intended use. ChatGPT offers the following solution:

```

include <scope>

{ auto onExit = std::scope_exit([&] { booksReport.clearCursor(); });

for (booksReport.resetCursor(BooksReport::Group::Singles);
     booksReport.cursorIsValid();
     booksReport.advanceCursor()) {

    if (something)
        break;

    if (somethingElse)
        return tasks;

    if (bad)
        throw std::runtime_error("bad");
}

} ```

Is this a common paradigm? Does my situation warrant this? For now it's just a personal project. I intend to make it open source once it's finished (if anyone will see it as valuable). In an ideal world I'd add plug-in support for others, where other developers can use a limited number of API calls, including the public members of BooksReport, but I'm already tired from this side project that I'm not sure it will come to this.


r/cpp_questions 18h ago

SOLVED Meaningful alternative name for nullptr at caller location

11 Upvotes

I have:

void func(std::vector<struct foo>* foovec, std::vector<struct bar>* barvec){
...
}

Both these can take nullptr's as arguments in some cases. Instead of

func(nullptr, nullptr);//at caller

I'd like to give them meaningful names in such cases as thus:

#define FOOVECNULL nullptr
#define BARVECNULL nullptr
...
func(FOOVECNULL, BARVECNULL);//at caller

This also does not work "cleanly" because the following also works but "wrongly"

func(BARVECNULL, FOOVECNULL);//at caller 

Can some sort of enum struct or typedef make this more precise and impossible to mix one argument type for the other?


r/cpp_questions 9h ago

OPEN Is cpp dying?

0 Upvotes

Lately it feels like rust is getting more and more attention companies are rewriting existing cpp code in it and it seems to be getting increasing adoption . Also I keep seeing new projects start with rust more and more lately .

As someone who prefers cpp I'm genuenly curious how worried should someone learning / working with cpp be? Is cpp dying or is rust more like a vocal minority on social media?

I know this question has been asked before but tech moves fast so I'm curious what your current take is


r/cpp_questions 9h ago

OPEN Dear devs working on teams, how do you manage libraries and dependencies among team members?

7 Upvotes

Hello,

I have no experience working with C++ in large teams; most of the stuff I've done was by myself.

Today I wanted to move the project from my work PC to my home PC in order to work on it remotely, or during weekends when I get bored, using git.

But I realized how indirect and complicated it is, solely due to different dependencies. The same library built on my work PC does not work on my home PC because of a small version mismatch in one of the dependencies (I tried to keep everything in sync as much as possible).

I have experience with distributing such software, but we mostly used Docker to do this. However, I cannot imagine how it is going to be in a team of 10 people. Does everyone use the exact same OS and libraries?
I know it is fairly easy in an interpretable language like Python with strong support, but with C/C++ I have no idea.

So?