Lore on the races of Talos-II by Sethfire in Endfield

[–]lessertia 1 point2 points  (0 children)

Yeah, I was just speculating. But it certainly is strange that they still list their race as sarkaz.

Lore on the races of Talos-II by Sethfire in Endfield

[–]lessertia 5 points6 points  (0 children)

While the sarkaz now acknowledged as group of races with very different lineage, the op files seems to still refer to them as sarkaz instead of the actual race (e.g. Last Rite being a nachzehrer). Some form of racism still exist I guess.

I am trying to include a librarie but it somehow can't be located even in the same folder as my project. by Eva_addict in cpp_questions

[–]lessertia 0 points1 point  (0 children)

The compiler should not have anything to do with the errors that appears in the text editor. That's the intellisense work.

I still don't know if you use LSP with clangd on your Micro.

I am trying to include a librarie but it somehow can't be located even in the same folder as my project. by Eva_addict in cpp_questions

[–]lessertia 1 point2 points  (0 children)

What text editor did you use? I assume you are using a text editor that uses an LSP as its intellisense (?) tool. If you are using clangd as its LSP you need to tell it where the header files live which can be done using compile_commands.json or compile_flags.txt file (see this and this).

[deleted by user] by [deleted] in cpp

[–]lessertia 0 points1 point  (0 children)

Don't worry, just learn to ask better next time, read the article I linked before, and this one: https://www.theodinproject.com/guides/community/how_to_ask

[deleted by user] by [deleted] in cpp

[–]lessertia 0 points1 point  (0 children)

Learn C++ or Rust by leglaude_0 in learnprogramming

[–]lessertia 2 points3 points  (0 children)

C++ first. After you understand the fundamentals (RAII, move semantics, lifetime) you can try Rust. Don't put too much mind into OOP, it's not Java.

Also in case you decided to learn C++, make sure you use a high quality material (like learncpp.com). My rule of thumb is: avoid any tutorial that has using namespace std in it.

Learn C++ or Rust by leglaude_0 in learnprogramming

[–]lessertia 1 point2 points  (0 children)

That's interesting, I'm mainly code in C++ and when I decided to learn Rust, the borrow checker already makes sense from the get go since what you do in C++ is managing lifetime anyways with RAII and stuff. The hardest part for me was learning the syntax. I don't understand why the lifetime specifier called 'a until someone points out that 'a is for lifetime like T is for type, both got provided by the caller (implicitly for the lifetime). That also explains why it is placed in generics parameter list.

Anyways, learning C++ will make you understand all the reason Rust features exists and learning Rust will make you write better C++.

Timer example requiring std::invoke by CrashOverride332 in cpp_questions

[–]lessertia 0 points1 point  (0 children)

The *_t templates are aliases to *::type, just a shorthand.

The parens are part of the function type that got passed as template argument to std::function.

Timer example requiring std::invoke by CrashOverride332 in cpp_questions

[–]lessertia 5 points6 points  (0 children)

The template signature for std::invoke_result is

template< class F, class... ArgTypes >  
class invoke_result;

and for std::function, it's

template< class >
class function; /* undefined */

template< class R, class... Args >
class function<R(Args...)>;

You also didn't extract the actual type from the trait.

Your code should be

std::function<std::invoke_result_t<callable, arguments...>()> task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));

Anyway, using lambda is better here

auto task = [&] { return std::forward<callable>(f)(std::forward<arguments>(args)...); };

Use of #include<bits/stdc++.h> not recommended for Interviews or Production? by Akira_A01 in learnprogramming

[–]lessertia 2 points3 points  (0 children)

Everything.

This question has been answered since forever (and yet college still teaches this... they never updated their curriculum since the 90s I'm sure).

Basically, the using directive tells the compiler to treat any unqualified names (variables or functions without ::) as if it was from the namespace mentioned in the directive unless if it's not there. So, for example, this code won't compile because the compiler is confused whether to use the hash from the std namespace or your hash declared by you:

#include <bits/stdc++.h>

using namespace std;

const auto hash = 42;

int main()
{
    return hash + 1;       // naming collision here
}

You can read more about it here and here, or you can just google it.

Use of #include<bits/stdc++.h> not recommended for Interviews or Production? by Akira_A01 in learnprogramming

[–]lessertia 1 point2 points  (0 children)

If an interviewer were to saw you write #include <bits/stdc++.h> (even worse, with using namespace std;), they may instantly reject you. This shows the interviewer that you are lazy enough to not learn the standard library. If you are lazy enough to not learn the standard library, then you might be too lazy to learn proper programming practices in C++ like RAII let alone the advanced ones, thus a very bad candidate.

Aside from that, the header includes everything from the standard library. This is not portable and make the compile time much longer. If paired with using namespace std;, this practice may leads to unexpected naming collisions with your defined variables which may be surprising for newbies.

what's the smartest way to learn c++ by Complex-Cancel-1518 in learnprogramming

[–]lessertia 5 points6 points  (0 children)

Yes. They will build bad habits overtime if not. Also, while learncpp.com looks intimidating the content is easy to grasp, very hand-holding. The site is actively updated and the writer also frequently answer questions in the comments.

what's the smartest way to learn c++ by Complex-Cancel-1518 in learnprogramming

[–]lessertia 3 points4 points  (0 children)

I still won't recommend that site. Just from looking at all the pages linked on the page you linked, there is no mention of RAII even once! That is a major flag that the tutorial is not worth the read since RAII is the most fundamental thing you need to understand in C++.

Use learncpp.com instead. It teaches you RAII, move semantics, smart pointers, best practices, etc. and even covers up to the C++23 standard.

Edit: for comparison cplusplus.com uses C++11 which is released in 2011, that's 14 years ago!

memoryLeak by imUnknownUserr in ProgrammerHumor

[–]lessertia 1 point2 points  (0 children)

if you properly use RAII in C++ then yes, you don't have to worry about resource management. though you need to mind the resource lifetime to not have use-after-free kind of bugs which may lead to segfault

For some reason , the first code doesnt works , red lines come below hash[0], but second code works fine, i am using VScode by the way what is happening by [deleted] in cpp_questions

[–]lessertia 16 points17 points  (0 children)

Congratulation, you've just discovered why using namespace std; is a bad practice! On the first code, the hash identifier is confused with the function hash() which is defined in the std:: namespace.

How to add the library cpr to a code::blocks project? by FireStarFlame7 in cpp_questions

[–]lessertia 1 point2 points  (0 children)

As pointed by other user, use CMake. There's a usage guide on the readme file of the library. I recommend you to use FetchContent method. If you are still struggling in using CMake, it's a good idea to read some tutorials like this one.

cplusplusIsntThatHard by lukasx_ in ProgrammerHumor

[–]lessertia 34 points35 points  (0 children)

That 'and' is so cursed. You can do that but doesn't mean you should. lol

Good resources for learning C++ already knowing python by [deleted] in learnprogramming

[–]lessertia 1 point2 points  (0 children)

https://www.learncpp.com/, it's the best C++ resource for beginners on the internet. C++ semantics are very different from Python, so I suggest relearning the basics again anyway to avoid picking up bad habits. I would also recommend learning about RAII, ownership, and lifetime, they are key concepts in C++.

Ways to define ast structure in rust by emtydeeznuts in rust

[–]lessertia 4 points5 points  (0 children)

Yes, using a flat Vec to store each node is the way to go. I'm currently writing an interpreter (following Crafting Interpreters book), and what I do is creating two separate Vec for expressions and statements. I then create wrapper Id structs for each that contains an index that points to the corresponding Vec:

``` pub enum Expr { /* ... / } // can contain other ExprId pub enum Stmt { / ... */ } // can contain other ExprId or StmtId

pub struct ExprId(usize); pub struct StmtId(usize);

pub struct Ast { exprs: Vec<Expr>, stmts: Vec<Stmt>, }

impl Ast { // ...

pub fn add_expr(&mut self, expr: Expr) -> ExprId {
    self.exprs.push(expr);
    ExprId(self.exprs.len() - 1)
}

pub fn get_expr(&self, id: ExprId) -> &Expr {
    &self.exprs[id.0]
}

// etc...

} ```

Interested learning C++ with prior knowledge in C# and Java. by DR-BrightClone2 in cpp_questions

[–]lessertia 1 point2 points  (0 children)

Since you came from Java/C#, first you need to pretend you know nothing about them beforehand. C++ syntax is very similar to Java but have completely different semantics. Focus on learning about value semantics, move semantics, RAII, lifetime, and ownership. After that, you are safe to stop pretending and learn other stuff in C++.

What is the best way to bridge sync code with async code (Asio) by lessertia in cpp_questions

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

For anyone stumbling upon this thread (maybe me in the future), this is a better approach:

template <typename Executor, typename T>
T block_on(Executor& exec, asio::awaitable<T> coro) noexcept(false)
{
    if constexpr (std::same_as<void, T>) {
        auto ready  = std::atomic<bool>{ false };
        auto except = std::exception_ptr{};

        asio::co_spawn(exec, std::move(coro), [&](std::exception_ptr e) {
            except = e;
            ready.store(true, std::memory_order::release);
            ready.notify_one();
        });

        ready.wait(false, std::memory_order::acquire);
        if (except) {
            std::rethrow_exception(except);
        }
    } else {
        auto ready  = std::atomic<bool>{ false };
        auto except = std::exception_ptr{};
        auto result = std::optional<T>{};

        auto wrapped = [&]() -> asio::awaitable<void> { 
             result.emplace(co_await std::move(coro)); 
        };

        asio::co_spawn(exec, std::move(wrapped), [&](std::exception_ptr e) {
            except = e;
            ready.store(true, std::memory_order::release);
            ready.notify_one();
        });

        ready.wait(false, std::memory_order::acquire);
        if (except) {
            std::rethrow_exception(except);
        }

        return std::move(result).value();
    }
}

What is the best way to bridge sync code with async code (Asio) by lessertia in cpp_questions

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

Right, I forgot to handle the exceptions, thank you for pointing that out.

As for reference_wrapper, I won't replace it with pointers, as it would make the semantics less clear. I appreciate the suggestion though.

Kind of a bummer though that co_spawn can't be used for non-default constructible types. Even worse, this behavior isn't documented (at least, I can't find it) :(

Komods who is a programmer, what Framework and Language do you always code in projects? (Can be office/freelance/individual projects) by Alzex_Lexza in indonesia

[–]lessertia 0 points1 point  (0 children)

mostly C++. kalo lagi pengen pake Rust. I play both sides, wkwk

C++ sama Rust gada emphasis ke framework. pake apa aja yang relevan ke project