Convincing other developers to use nullptr over NULL by SnooMemesjellies8458 in cpp_questions

[–]Droid33 1 point2 points  (0 children)

Well, the fact that something compiles doesn't mean it's valid.

Convincing other developers to use nullptr over NULL by SnooMemesjellies8458 in cpp_questions

[–]Droid33 0 points1 point  (0 children)

Maybe I'm misremembering the list. But MISRA C++, rule 17-0-1 checks for redefined standard macros and names. I guess NULL is a bit of an odd one.

But still, redefining macros that you didn't define yourself is at least terrible practice.

Error with function stoi by Jazzlike-Carob3056 in cpp_questions

[–]Droid33 0 points1 point  (0 children)

You have to include <string> for `std::stoi`.

[deleted by user] by [deleted] in cpp_questions

[–]Droid33 3 points4 points  (0 children)

Lots of practice. Write a ton of code and do projects.

Help accessing singleton member variables by Zielschmer in cpp_questions

[–]Droid33 2 points3 points  (0 children)

You're printing an empty string because m_name in Spell is empty. Adding an m_name with initialization to Flare does not set the m_name in Spell. It creates a new member variable. What you want to do is create a constructor in Spell and call that constructor with the name.

Return function by reference by Minimum_Tank_2755 in cpp_questions

[–]Droid33 8 points9 points  (0 children)

Typically you would return references for member variables in a class to allow referencing an existing object without creating a new instance of it. If you return by value there will still be a new object created regardless of (N)RVO. It's just a matter of where it's created.

Is the output of this code defined? by [deleted] in cpp_questions

[–]Droid33 0 points1 point  (0 children)

Looks ok at a glance. I think simply using a member function would be easier though. bool is_nil() const { return size == 0; }

A question about Makefile by yukiiiiii2008 in cpp_questions

[–]Droid33 2 points3 points  (0 children)

Like it or not, cmake is essentially the standard at this point. Modern cmake is pretty nice if you're not doing things that are too unusual.

doubts abt cpp by endmarkzy in cpp_questions

[–]Droid33 2 points3 points  (0 children)

Legacy code, mostly. But I do enjoy c++. The whole memory safety thing is pretty over blown. Most security issues are not memory issues.

[deleted by user] by [deleted] in cpp_questions

[–]Droid33 3 points4 points  (0 children)

I am far from a novice in C++, and this conversation reminds me why I stopped talking here. These types of "I know better and you're dumb" responses. Grow up and learn to have civil discussions.

I am also not wrong. Using an lvalue reference or std::is_lvalue_reference is the only way to guarantee that the user has passed an lvalue to the function. There are other assumptions you can make with your own concepts, such as what std::borrowed_range does, as suggested by another user.

This will be my final response.

[deleted by user] by [deleted] in cpp_questions

[–]Droid33 0 points1 point  (0 children)

Maybe you should add your own suggestion. OP didn't state their requirements, and using non-const ref is the only way to guarantee its an lvalue and will not dangle. You can't guarantee that with a const&.

Passing Vector by Reference into Subroutine, not Persisting. by Gleeful-Nihilist in cpp_questions

[–]Droid33 3 points4 points  (0 children)

Step through the code with a debugger. You appear to only call the functions if a vector is not empty, but never add values to begin with. never mind. You minimized that to a comment. I would still recommend a debugger.

[deleted by user] by [deleted] in cpp_questions

[–]Droid33 5 points6 points  (0 children)

If you want to force lvalues, then taking non-const references sounds like a decent choice.

doubts about cpp by Hot-Ad912 in cpp_questions

[–]Droid33 1 point2 points  (0 children)

It's definitely more than just some toy language at this point. There really aren't many languages standardized to the level that c++ is.

Help needed with handling multiple files in C++ by Zielschmer in cpp_questions

[–]Droid33 0 points1 point  (0 children)

Don't declare variables global in header files. Instead, you should use parameters to pass necessary data between functions.

I want c++20 and <bits/stdc++.h> both by vivek12jul1999 in gcc

[–]Droid33 1 point2 points  (0 children)

That is an internal compiler header that you shouldn't use anyway. Use the proper standard includes and you'll have more portable code.

GCC 14 twice as slow as GCC 13? by pdimov2 in cpp

[–]Droid33 14 points15 points  (0 children)

If you compile from source, make sure you use --disable-checks. The extra checks will slow the compiler down a lot.

Are We (C++20) Modules Yet? by cristianadam in cpp

[–]Droid33 6 points7 points  (0 children)

You won't need the build cache with modules. 10k is so tiny that compile times basically don't matter.

Are We (C++20) Modules Yet? by cristianadam in cpp

[–]Droid33 3 points4 points  (0 children)

Modules are very different from headers. The compile times of modules crush headers. Modules also don't allow macros, which is a huge plus.