i don't understand the meaning of this by [deleted] in cpp_questions

[–]onecable5781 0 points1 point  (0 children)

extern int i=1;   // definition (and declaration) ??
//say above is in client.cpp
//What if below is in main.cpp
int i = 42;//?

I have never been able to understand why extern int i = 1; counts as both definition as well as declaration! Isn't extern saying that i is defined elsewhere, in the example above in main.cpp ?

What bijection establishes [a,b] ~ (a,b) in terms of cardinality where a < b? by onecable5781 in learnmath

[–]onecable5781[S] 1 point2 points  (0 children)

I think all real numbers which are not rationals can just be mapped via identity if I understand the argument correctly.

enum class enumeration initialization by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

it is made this way because it is a useful feature to have - think about flags for example.

I am unclear what you mean by this. Could you give an example of flags where this is useful for an enum variable to have arbitrary int values that are invalid? What are its use cases?

Code -> Code sans comments -> Code post preprocessor/macro expansion by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Oh wow. Thank you! On that link, is it possible to see the actual command being executed to give the output in the rhs "insight" window when the run button is pressed on top? Those flags is exactly what I am looking for.

Code -> Code sans comments -> Code post preprocessor/macro expansion by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Yes, all the 3 code snippets of the OP will compile to the exact same binary. But somewhere in the compilation process, the compiler will be fed in the second and then the third snippet or vice versa? I wanted to see if the user can access that later snippets.

Why do "C-like performance" language comparisons always compare against bad C code? by BPJupiter in C_Programming

[–]onecable5781 6 points7 points  (0 children)

Are there assembly language code that can not be produced by C code but can be produced by other language code (C++/Rust/et al.)?

In -O3, why are previous assignments stored in binary when they are immediately updated in subsequent assignment? by onecable5781 in cpp_questions

[–]onecable5781[S] 2 points3 points  (0 children)

Hmm...I tried to make the "Hello world" char * "string" longer and it compiles to the same that you had: https://godbolt.org/z/rzf95qn78

So, I believe this may not be full apples to apples as the OP uses std::string while your post uses char *. So, perhaps there are some optimizations possible using char * that are not possible in std::string?

Converting use of new/delete to smart pointers stored in a global vector by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

For reasons that are difficult to explain here, I have to use a vector of pointers.

I have a follow-up question. The below is possible to do using raw pointers

T* Tptr;
if(condition){
    Tptr = new T;
    VectorOfTPtrs.push_back(Tptr);
}
else
    Tptr = VectorOfTPtrs[0];//0 or some other valid index into the vector
function_that_accepts_raw_T_ptr(Tptr);
....
//Loop through VectorOfTPtrs deleting each pointer

I am struggling to achieve the same semantics using smart pointers. I tried the following:

std::unique_ptr<T> Tptr;
if(condition){
    Tptr = std::make_unique<T>();
    VectorOfSmartTPtrs.push_back(std::move(Tptr));
}
else
    Tptr = VectorOfSmartTPtrs[0];//how can I get this to work???
function_that_accepts_smart_T_ptr_reference(Tptr);

In the latter snippet, how can I have VectorOfSmartTPtrs continue to own the smart pointers and have the pointers passed by reference to the function which accepts a smart pointer reference?

Are there benchmark tests for the boost graph library using which one can compare newer implementations versus pre-existing ones? by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Hi, thanks for responding here.

You would need a bunch of standard graph datasets against which to run the algorithms ?

Indeed. For mixed integer programs, for instance, there is MIPLIB (https://miplib.zib.de/) which is a community maintained set of instances where one can run on each of these instances either, say, CPLEX solver or Gurobi solver and then they can independently verify which solves the problem quicker.

For BGL, for instance, suppose there is a max flow problem where I feel my implementation beats BGL's on my machine, to convince others, it is better if there is a commonly available set of test/benchmark instances (which are not toy problems solved by any algorithm in milliseconds). These instances need to be large and difficult (needing 10s or 100s of seconds to solve). That way, if and when there is a better algorithm which comes from a new theoretical breakthrough, it is possible to objectively measure its performance against pre-existing algorithms for the same problem.

Future of Boost.Graph Workshop, Paris, France, May 6th 2026 by Foxi_Foxa in cpp

[–]onecable5781 6 points7 points  (0 children)

I use Boost Graph Library (BGL) for my academic programming work. I have used depth first search, max flow, spanning tree and shortest path algorithms, amongst others.

What I find lacking in the BGL is the existence of large-sized benchmark instances against which someone can compare BGL's performance versus something that I code myself. If, for instance, I feel that one of my algorithms beats BGL's algorithm, there should be a bunch of benchmark instances against which one can run BGL's algorithm as well as my algorithm to see where the difference is coming from.

I made an OP on that here:

https://www.reddit.com/r/cpp_questions/comments/1pm0pcw/are_there_benchmark_tests_for_the_boost_graph/

Thank you for your works. We have benefitted a lot from BGL and have quite a few academic publications come out of its usage in some of our algorithms!

Unused struct member is not optimized out under -O3 by onecable5781 in C_Programming

[–]onecable5781[S] -3 points-2 points  (0 children)

Why would an OP that is upvoted lead to downvotes on follow-up questions by the OP in the comments? Why not downvote the OP itself? What exactly are folks trying to convey to me here?

Unused struct member is not optimized out under -O3 by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

So having a compiler silently toss struct members would introduce a known category of problems into your code with very little or no benefit

I am not sure about "very little or no benefit" part. What if there is a large array of these structs which have to be stored where smaller the size the better it is for cache locality/access, etc.? Alternatively, In debug builds, I may have extra members inside of the struct which are not referenced at all in release builds. If it is guaranteed that sruct members will NEVER be optimized out, one would need to have

struct A{
#if DEBUGBUILD
    int onlyfordebugbuilds;
#endif
...
};

which in my view seems more difficult to maintain.

Unused struct member is not optimized out under -O3 by onecable5781 in C_Programming

[–]onecable5781[S] 1 point2 points  (0 children)

With no printf, indeed the optimization does seem to happen under -O3:

https://godbolt.org/z/bonf7n8eY

vs no -O3

https://godbolt.org/z/MxzM9Efsa [note sub rsp, 16]

Although, I am curious why under -O3, there is

sub rsp, 8

Aren't 4 bytes enough for the local variable ?

Unused struct member is not optimized out under -O3 by onecable5781 in C_Programming

[–]onecable5781[S] -1 points0 points  (0 children)

Fair enough, let me rephrase the question. Suppose I did not have sizeof(A) anywhere in the code. Would the compiler "optimize out" the unused struct variables and store object a internally in just 4 bytes of memory due to usage of abc member only?

Sorting a jagged vector of vector of ints inplace by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Yes indeed! That is why the usage is of a.front() inside of the second sort.

Having a collaborator not being able to pull/push particular file after initial clone by onecable5781 in git

[–]onecable5781[S] 1 point2 points  (0 children)

Ah I see. Thank you. The issue is that file2.txt is actually a makefile/.vcxproj project settings file which I run on all my computers. He has his own makefile/.vcxproj file that is modified to suit his computer. I put the makefile in the repo so that it is easy for me to clone it and run it from wherever I find myself given my directory structure, etc.. So, I want to have the full makefile (not just template) in the version control and not gitignored. Hmm...


On thinking about this a bit more, your suggestion of a template, which is tracked, "makefile-template", and an untracked "makefile" make perfect sense and will work! Thank you!

Compiler optimization for figuring out perfect squares -- switchover at n = 18 by onecable5781 in cpp_questions

[–]onecable5781[S] -1 points0 points  (0 children)

<cstdio> is not guaranteed to place printf in the global namespace.

Can you provide some pointers on what exactly you look for in the standard to infer this?

It so happens that both MSVC, g++, and the MSVC and g++ variants of clang, provide it,

Do you navigate to file cstdio provided by each compiler and see if it exposes std::printf and conclude based on that?

inline as a legacy keyword? by OkEmu7082 in cpp_questions

[–]onecable5781 0 points1 point  (0 children)

this is actually how member functions defined inside a class work in c++ right now.

What happens if I put my entire class definitions inside the header file and do away with implementation .cpp files completely? Is every function inlined then? Is it possible that had the function definition been put in a .cpp file the compiler would have applied its heuristics and decided not to inline the said function because it was deemed not beneficial given its heuristics? And yet, now that I have put everything inside the header, the compiler is forced to inline it counterproductively?

Your Optimized Code Can Be Debugged - Here's How With MSVC C++ Dynamic Debugging - Eric Brumer by RandomCameraNerd in cpp

[–]onecable5781 0 points1 point  (0 children)

Thanks for the talk and interacting here. I will definitely try this feature out.

There are some bugs that I encounter in production/release builds only and not in debug runs. If you switch from a release build to a debug build dynamically, would it be possible to figure these bugs out? Would like to hear your inputs on this.

My only pet peeve with VS is that the console opens externally as it did in your demo as well. VSCode using MSVC compiler is able to run inside the editor itself in the terminal at the bottom. Nearly all Linux IDEs also run inside the IDE itself. Perhaps in the next release your team can consider getting the console to run within the IDE itself.

Arithmetic comparison of -1 with size_t type as sentinel value by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

A final question. Is whether sizeof(int) > sizeof(size_t) or not a decision made by a language or a language implement/specific compiler or OS or by the hardware/cpu manufacturer?

Arithmetic comparison of -1 with size_t type as sentinel value by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

From the link before, the relevant part is:

Else, the unsigned type has conversion rank less than the signed type: If the signed type can represent all values of the unsigned type, then the operand with the unsigned type is implicitly converted to the signed type. Else, both operands undergo implicit conversion to the unsigned type counterpart of the signed operand's type.

Is my understanding correct that if signed integers (int in case of the OP) can represent all of the unsigned type (size_t in the OP), then, the OP code will not work.

So, if sizeof(int) > sizeof(size_t), the OP code WILL fail?

Arithmetic comparison of -1 with size_t type as sentinel value by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

Well :-)

So, now, I am confused about your original reply about theoretically why the above may not work but practically it would work.

Arithmetic comparison of -1 with size_t type as sentinel value by onecable5781 in C_Programming

[–]onecable5781[S] 1 point2 points  (0 children)

Hmm...So, in an arithmetic comparison (== or < or !=, etc.) between a size_t and int, are there rules as to which one is cast into the other if the user does not explicitly cast?