snippetcppMinor
How do I make this simple addition code less verbose?
Viewed 0 times
thissimplemakeadditionlesshowcodeverbose
Problem
I really don't get why it has to take so much to make such a little function. Here's what I have.
Is there a way to shrink this code while maintaining its ability to take different arguments? For a comparison, here is it in Python; there is a big difference between the two.
template auto add(A a, B b) -> decltype(a + b) {
return a + b;
}Is there a way to shrink this code while maintaining its ability to take different arguments? For a comparison, here is it in Python; there is a big difference between the two.
def add(a, b):
return a + bSolution
You may want to take a look at Pythy.
What you want seems a lot like:
Make sure to read this part of the article, as pointed out by Konrad:
It appears that we are derefencing a null pointer. Remember in C++
when dereferencing a null pointer, undefined behavior occurs when
there is an rvalue-to-lvalue conversion. However, since a
non-capturing lambda closure is almost always implemented as an object
with no members, undefined behavior never occurs, since it won't
access any of its members. Its highly unlikely that a non-capturing
lambda closure could be implemented another way since it must be
convertible to a function pointer. But perhaps not?
What you want seems a lot like:
PYTHY(add, x, y) (
return x + y;
)Make sure to read this part of the article, as pointed out by Konrad:
It appears that we are derefencing a null pointer. Remember in C++
when dereferencing a null pointer, undefined behavior occurs when
there is an rvalue-to-lvalue conversion. However, since a
non-capturing lambda closure is almost always implemented as an object
with no members, undefined behavior never occurs, since it won't
access any of its members. Its highly unlikely that a non-capturing
lambda closure could be implemented another way since it must be
convertible to a function pointer. But perhaps not?
Code Snippets
PYTHY(add, x, y) (
return x + y;
)Context
StackExchange Code Review Q#15504, answer score: 5
Revisions (0)
No revisions yet.