Skip to content

Instantly share code, notes, and snippets.

View debbynirwan's full-sized avatar
🏠
Working from home

Debby Nirwan debbynirwan

🏠
Working from home
View GitHub Profile
constexpr int Sum(const int a, const int b) {
return a + b;
}
constexpr int main(int argc, char **argv) {
const int val = Sum(1, 2);
return 0;
}
int main() {
constexpr int val = 1 + 2;
return 0;
}
int Sum(const int a, const int b) {
return a + b;
}
int main(int argc, char **argv) {
const int val = Sum(1, 2);
return 0;
}
int main() {
const int val = 1 + 2;
return 0;
}
void callback(const int *value) {
int *mutableValue = const_cast<int*>(value);
// we can now modify value, but the result is undefined
*mutableValue = 100;
std::cout << "mutableValue: " << *mutableValue << "\n";
}
void library_code() {
const int value = 10;
callback(&value);
void callback(const int *value) {
int *mutableValue = const_cast<int*>(value);
// we can now modify value
}
void library_code() {
int value = 10;
callback(&value);
}
void callback(int *value) {
const int *immutableValue = const_cast<const int*>(value);
// from this point
// we use immutableValue to ensure const correctness
}
void library_code() {
int value = 10;
callback(&value);
}
Output Process(const Input *input) {
return Output();
}
int main() {
Input input;
Output output = Process(&input);
return 0;
}
int main() {
Input input;
const Input *input2 = &input;
Output output = Process(*input2);
return 0;
}
Output Process (Input input) {
return Output();
}
int main() {
Input input;
const Input& input2 = input;
Output output = Process(input2);
return 0;