Skip to content

V for CPP developers

Turiiya edited this page Apr 10, 2024 · 14 revisions
Hello World
#include <iostream>
int main() {
	std::cout < "Hello World!" < "\n";
}
fn main() {
	println('Hello World!')
}
Vector initialization
std::vector<int> numbers = {1, 2, 3, 4};
numbers := [1, 2, 3, 4]
Add an element to a vector
numbers.push_back(5);
numbers << 5
Printing a vector
std::copy(numbers.begin(), numbers.end(),
	std::ostream_iterator<int>(std::cout, " "));
std::cout < '\n';
println(numbers)
Filtering a vector
std::copy_if(numbers.begin(), numbers.end(),
	std::back_inserter(bar),
	[](int i){ return i % 2 == 0; });
numbers.filter(it % 2 == 0)
Reading a file
#include <iostream>
#include <fstream>
std::ifstream f("path");
std::string text;
	text.assign(std::istreambuf_iterator<char>(f), {});
	if (!f)
		std::cerr < "error reading from file\n";
import os

text := os.read_file(path) or {
	eprintln(err)
}
Monomorphic function
template <typename T>
auto simple_fn(T param) {
	return param;
}
auto value = simple_fn<int>1);
fn simple_fn[T](param T) T {
	return param
}

value := simple_fn(1)
Monomorphic struct
template <typename T>
struct generic_struct {
	T value;
	void generic_method();
};

template <typename T>
generic_struct<T>:generic_method() {
return;
}
struct GenericStruct[T] {
	value T
}

fn (g GenericStruct[T]) generic_method() {
	return
}
Type reflection
#include <iostream>
using Number = int;

template <typename T>
consteval bool is_number(T data) {
	if constexpr (std::is_same<T, Number>()) {
		return true;
	} else {
		return false;
	}
}

std::cout << is_number<Number>(0) << "\n";
type Number = int

fn is_number[T](data T) bool {
	$if T is Number {
		return true
	} $else {
		return false
	}
}

println(is_number(Number(0)))
Standard map
#include <iostream>
#include <map>

std::map<std::string, int> my_map {
	{"KEY_1", 0},
	{"KEY_2", 10},
};

for (const auto &[key, value] : my_map) {
	std::cout << key << ": " << value << ", ";
}
std::cout << "\n";
my_map := map {
	'KEY_1': 0
	'KEY_2': 10
}

println(my_map)