Posts

Showing posts with the label if-statement

Julia ternary operator without `else`

4 Consider the ternary operator in Julia julia> x = 1 ; y = 2 julia> println(x < y ? "less than" : "not less than") less than Question: Is there a way to omit the : part of the statement ? Something which would be equivalent to if condition # dosomething end without writing that if the condition is not met, nothing should be done. NB: I researched the answer but nothing came out, even in related questions (1, 2) if-statement syntax conditional-statements julia conditional-operator Share ...

Do I need to put constexpr after else-if?

Inspired by this answer, I tried to copy and paste (and add testing in main()) this code: template<typename T> std::tuple<int, double> foo(T a) { if constexpr (std::is_same_v<int, T>) return {a, 0.0}; else if (std::is_same_v<double, T>) return {0, a}; else return {0, 0.0}; } int main() { auto [x, y] = foo(""); std::cout << x << " " << y; } This is very straightforward - if T is deduced as int, we want to return a tuple of [a, 0.0]. If T is deduced as double, we want to return a tuple of [0, a]. Otherwise, we want to return [0, 0.0]. As you can see, in the main() function, I am calling foo with const char* argument, which should result in x and y being 0. That is not the case. While trying to compile it, I encountered a strange error: error: could not convert '{0, a}' from '<brace-enclosed initializer list>' to 'std::tuple<int, double>' And I w...