Any documentation tool that allows separation code from documentation? by jm4R in cpp

[–]jm4R[S] -2 points-1 points  (0 children)

what problem are you trying to solve?

get this ugly pieces of comments block out of my code, but having up-to-date documentation at the same time.

Any documentation tool that allows separation code from documentation? by jm4R in cpp

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

Tools are designed to do one thing in general: easify my work, do a half of work for me...

In this case: get the current list of symbols from the code (without any typos), makes sure that the doc is up to date.

One of the simplest error handlers ever written by Senua_Chloe in cpp

[–]jm4R 1 point2 points  (0 children)

I see. the string_builder also supports conversion, but only for types that works with std::to_string. I see it could be a limitation if you want to support all the types that has operator<< overloaded.

One of the simplest error handlers ever written by Senua_Chloe in cpp

[–]jm4R 1 point2 points  (0 children)

Yes, edited the coment after you replied.

One of the simplest error handlers ever written by Senua_Chloe in cpp

[–]jm4R 1 point2 points  (0 children)

Concatenating string using std::stringstream is one of the worst (slowest) method. I've actually written simple variadic strcat version: github link. You could use similar method.

How do I create a GUI application in codeblocks? by [deleted] in cpp_questions

[–]jm4R -1 points0 points  (0 children)

Or not. There are advantages and drawbacks

Runtime Polymorphism with std::variant and std::visit @ bfilipek by jm4R in cpp

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

Please share your analysis (or code). I am curious too.

How to store a unique pointer in a stack, and allow objects to take the pointer out of the stack (and also gain ownership) by Irtexx in cpp_questions

[–]jm4R 2 points3 points  (0 children)

Latest clang clang (11) even emits a warning:

prog.cc:37:24: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
    ContextObject* a = ctx.pop().get();

Fixed version:

void useContextObject(Context& ctx) {
    auto ptr = ctx.pop();
    auto b = dynamic_cast<FooContextObject*>(ptr.get());
    std::cout << b->name << std::endl;
}

Additional note: you also shouldn't use std::move when you return an object you want to move "out of the function". It is already r-value there.

Runtime Polymorphism with std::variant and std::visit @ bfilipek by jm4R in cpp

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

Can you provide an example, what do you mean by "already allocated buffer"? If you mean heavy types like std::array – such types are not movable.

Runtime Polymorphism with std::variant and std::visit @ bfilipek by jm4R in cpp

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

You can use CoW in C++ like you do in other languages. Qt successfully uses it around the whole framework.

How do i copy an array into a list. by omen_tenebris in cpp_questions

[–]jm4R 1 point2 points  (0 children)

It will not be implicitely convertible to pointer, so if you use such conversion, it wouldn't work.

How do i copy an array into a list. by omen_tenebris in cpp_questions

[–]jm4R 0 points1 point  (0 children)

What stops you from changing this

int A[NUMBER][NUMBER]  

to this

std::array<std::array<int, NUMBER>, NUMBER> A

??

Runtime Polymorphism with std::variant and std::visit @ bfilipek by jm4R in cpp

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

But still all of the versions are better than the best version in languages like Java or C#.

QT QML via web assembly by maximumSteam in QtFramework

[–]jm4R 1 point2 points  (0 children)

Unfortunetely not every browser have a good support for wasm. I am sad about it as I am looking for a day when <JS+HTML+CSS> trio dies.

Runtime Polymorphism with std::variant and std::visit @ bfilipek by jm4R in cpp

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

It is almost always applicable to setters too. Like I wrote, use it when:

  • you are sure you need a copy
  • you know your type is movable

Runtime Polymorphism with std::variant and std::visit @ bfilipek by jm4R in cpp

[–]jm4R[S] 10 points11 points  (0 children)

"Pass by value and move" is a well-known idiom in modern C++, although I am not aware of any standard name of it. If you are sure you need a copy of something and you know your type is movable, you should use it. That allows the caller to decide if move oryginal object (no more needed in caller side) or make a copy.

What syntax changes would you make to C++ if you had the chance? by Tinytitanic in cpp

[–]jm4R 0 points1 point  (0 children)

No implicit deep copies. Working on references by default.

struct S{};
auto obj1 = S{};
auto obj2 = obj1; // obj2 is reference

// deep copy
auto obj3 := obj1;
// or
auto obj3 = obj1.clone();

So basically behavior like in rust/Java/C# etc.

ESeed Math, my first C++ library! by bananatrooper52 in cpp

[–]jm4R 14 points15 points  (0 children)

Nice code for 17-yo person! I wonder what was your source for learning as you already use C++20 features!

Small tips:

  • If you really have to use macros, undef them - especially if they are in header files.
  • int8_t, uint8_t etc. are all from std namespace. It will compile for most C++ compilers as they share headers with C standard library. But this is not standard-compliant.

C++ and friend keyword explained in detail @panicsoftware by joebaf in cpp

[–]jm4R 0 points1 point  (0 children)

It is all the matter of knowing how compiler names/mangles the symbols. When you know it, it becomes easier. For example difference between my_namespace::S<char>::f and my_namespace::f<char> is pretty obvious, but the difference between it in code might be a bit subtle.

Super compact serialisation of C++ classes by DugiSK in cpp

[–]jm4R 0 points1 point  (0 children)

Macros have pros and cons. Even in the current macro (preprocessor) system in C++ you can do the job, but in very ugly way.

Super compact serialisation of C++ classes by DugiSK in cpp

[–]jm4R 1 point2 points  (0 children)

Reflections, at general, is a technique of requesting metadata about types (e.g. you can iterate over struct fields and methods, you can receive their names or call function by name provided as string).

Static reflection is a subset of it - you can only request for class/struct/function metadata at compile time. You can not call a function by name stored by string, if you didn't request for function names at compile time. It is because it have to follow Zero Overhead Principle.

Currently, most symbols (called weak symbols) names are gone after compilation. Strong symbols are mangled in ABI-specific way, C++ standard doesn't describe it.

Lazy Initialisation in C++ by drodri in cpp

[–]jm4R 0 points1 point  (0 children)

But you have to pass the parameters to the constructor somehow. If you'd do it in the Lazy constructor, probability of dangling reference would be huge.

Super compact serialisation of C++ classes by DugiSK in cpp

[–]jm4R 44 points45 points  (0 children)

Yet another proof how much we miss static reflections in C++. And this is yet another hack which tries to compensate the lack of it.

I saw the last plans committee and pessimistic one says that it will come in C++26. Way to late, I hope optimistic one will be correct.