[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip / qa] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: 1717430022949207.png (1.44 MB, 1840x1040)
1.44 MB
1.44 MB PNG
Are there any valid reasons why C++ shouldn't be used for all applications?
>>
>>103221173
Usually the most important thing to optimize is ease of development. In practical terms that means an OO HLL.
>>
>>103221173
300 keywords
>>
>>103221173
None at all.
>>
>>103221173
in c++, have you ever tried saving or loading a class that contains a vector to disk? or transmitting over network?
>>
>>103221173
C++ is an abomination designed specifically to torture programmers. After using it for over a decade I finally had enough and went back to C.
>>
>>103221173
>>103221895
C and C++ are Worse is Better.
>>
>>103221939
C++ is all worst, and no better. Every single feature in C++ is expertly designed to be just barely useful. It gets them so hard to take what should be a simple feature, and twist into some barely useful abomination that only solves a fraction of the issues programmers had, such that they can add a dozen more barely useful features. Making the language massively complex and crippled.

The quintessential example of worhtless features of C++ is constexpr if vs static if.
>>
File: It's_All_So_Tiresome.jpg (47 KB, 860x615)
47 KB
47 KB JPG
>>103221992
don't worry bro, we also have consteval in case you could still make sense of things before then (and imagine the fact that optimizing C compilers could eliminate branches without any of this).
>>
>>103221818
Yes (it's trivial, lmao).
>>
>>103222219
I'm genuinely curious because I have not found a good solution
how would you serialize this:
struct Element
{
int id;
std::string whatever;
}
struct Drawer
{
std::vector<Element> elements;
float average_data;
std::string name;
}
>>
>>103222451
If you cannot do it, you shouldn't be programming anything ever.
>>
>>103222451
In Rust this is just
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct Element {
id: i32,
whatever: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct Drawer {
elements: Vec<Element>,
average_data: f32,
name: String,
}

use bincode::{serialize, deserialize};
use std::fs::File;
use std::io::{Write, Read};

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create sample data
let drawer = Drawer {
elements: vec![
Element { id: 1, whatever: "First".to_string() },
Element { id: 2, whatever: "Second".to_string() },
],
average_data: 3.14,
name: "My Drawer".to_string(),
};

// Serialize
let encoded: Vec<u8> = serialize(&drawer)?;

// Write to file
let mut file = File::create("drawer.bin")?;
file.write_all(&encoded)?;

// Read from file
let mut file = File::open("drawer.bin")?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;

// Deserialize
let decoded: Drawer = deserialize(&buffer)?;

println!("Deserialized data: {:?}", decoded);

Ok(())
}
>>
>>103222499
I know how to do it
There's a couple different ways:
>You could get a json library, step through everything in your struct and convert it to an "Any" and create a tree of "Any", and then convert your "Any" tree into json
>You could see if your thing fits into something standard text format like a .csv
>You could make your own binary format (I've done this a couple times)
>You could try to overload << string streams and sanitize all your string members
>Rarely, if your data does not have any pointers and is all in contiguous memory and is fixed size, you could bit cast it

now compare this to most languages, where if you use standard data structures, you can just call something along the lines of
Json file_data = jsonify<Drawer>(my_drawer)
That's it.
In C++, every time I add a member to a struct, I have to make sure I add it to the struct_save and struct_load functions
>>
>>103222575
not my problem, I only care about performance, everything you listed immediately is too slow and retarded
>>
>>103222582
>Making structs that are fixed size and no pointers so you can bit cast them straight to disk
>slow
How tf do you write to disk then?
>>
>>103222451
By making structs fixed size. If there's pointers in your struct (eg a linked list or tree or w/e) then walk the pointers as you're casting to bytes and keep track of number of elements at the top of main struct so you can always compute the offset of the nth element depending on what data structure you're using.
>>
>>103222618
you cannot just "bit cast" into disk.
>>
>>103222696
Your example works for a simple vector
This doesn't work for the example, it's a vector of strings. I've done it before by creating a table of offset pointers into a big blob of bytes
This is inventing a new binary format for every single user defined data type by creating a read and write function
>>
>>103222575
there are c++ libraries that can do this
>>
>>103224601
If you're so lazy, why not use sqlite?
>>
>>103222451
newbie here why wouldn't this work with normal json marshaling?
>>
>>103225422
Share please?



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.