Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Experimenting with Value Structs...
all: test-template test-structs test-raw \
assembly-raw-float.s assembly-value-struct.s \
raw-vs-struct.diff
test-template: vstr-template.cpp
g++ -O2 -Wall -Wextra -o test-template vstr-template.cpp
test-structs: value-struct.cpp
g++ -O2 -Wall -Wextra -o test-structs value-struct.cpp
test-raw: raw-float.cpp
g++ -O2 -Wall -Wextra -o test-raw raw-float.cpp
assembly-raw-float.s: raw-float.cpp
g++ -O2 -Wall -Wextra -S -o assembly-raw-float.s raw-float.cpp
assembly-value-struct.s: value-struct.cpp
g++ -O2 -Wall -Wextra -S -o assembly-value-struct.s value-struct.cpp
raw-vs-struct.diff: assembly-raw-float.s assembly-value-struct.s
diff --side-by-side assembly-raw-float.s assembly-value-struct.s > raw-vs-struct.diff || true
#include <iostream>
float to_rad(float d){
return d * 3.14 / 180;
}
float to_deg(float r) {
return r * 180 / 3.14;
}
int main() {
float half_pi = 1.57;
float ninety_degrees = to_deg(half_pi);
std::cout << ninety_degrees << std::endl;
return 0;
}
#include <iostream>
struct Radians {
private:
float _value;
public:
Radians(float v) {
this->_value = v;
}
float value() {
return _value;
}
};
struct Degrees {
float value;
Degrees(float v){
this->value = v;
}
};
Radians to_rad(Degrees d){
return d.value * 3.14 / 180;
}
Degrees to_deg(Radians r) {
return r.value() * 180 / 3.14;
}
int main() {
Radians half_pi = Radians(1.57);
Degrees d = to_deg(half_pi);
std::cout << d.value << std::endl;
return 0;
}
#include <iostream>
template<typename T>
class ValueContainer {
private:
T _value;
public:
ValueContainer(T v) : _value(v){}
T value(){ return _value; }
};
using Degrees = ValueContainer<float>;
using Radians = ValueContainer<float>;
Radians to_rad(Degrees d){
return d.value() * 3.14 / 180;
}
Degrees to_deg(Radians r) {
return r.value() * 180 / 3.14;
}
int main() {
Radians half_pi = Radians(1.57);
Degrees d = to_deg(half_pi);
std::cout << d.value() << std::endl;
// does not change much...
Degrees e = to_deg(d);
std::cout << e.value() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.