Date: Thu, 21 Oct 2021 20:14:44 +0530
Not sure if there is a need for language features for every trivial thing
that can be solved by macro.
#include <iostream>
#define CONSTEXPR_TERNARY(C, A, B) [&] { \
if constexpr (C) { return (A);} \
else { return (B); } \
}();
int main() {
auto x = CONSTEXPR_TERNARY(true, 5, "ABC");
std::cout << x << std::endl;
auto y = CONSTEXPR_TERNARY(false, 5, "ABC");
std::cout << y << std::endl;
}
https://godbolt.org/z/11911TT3c
On Thu, Oct 21, 2021 at 7:37 PM Hui Xie via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Yes, having more little functions is considered better practice. But
> the code still looks pretty much the same as my original code in terms of
> lines of code and structure of the code. I think even with a separated
> more_code function, having a ternary operator can make constexpr code more
> like the way we wrote time code
>
> template <typename T>
> void f(T const& t){
> const auto obj = pred<T> constexpr? makeFoo() : makeBar();
> more_code(obj);
> }
>
> On Thu, Oct 21, 2021 at 2:06 PM Nikolay Mihaylov <nmmm_at_[hidden]> wrote:
>
>> what about restructuring like this:
>>
>> template<typename T>
>> void more_code(T a);
>>
>> template <typename T>
>> void f(T const& t){
>> if constexpr (pred<T>){
>> return more_code(makeFoo());
>> } else {
>> return more_code(makeBar());
>> }
>> }
>>
>>
>> On Thu, Oct 21, 2021 at 3:49 PM Hui Xie via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> Hi,
>>>
>>> Sometimes I have this situation
>>>
>>> template <typename T>
>>> void f(T const& t){
>>> const auto obj = []{
>>> if constexpr (pred<T>){
>>> return makeFoo();
>>> } else {
>>> return makeBar();
>>> }
>>> }();
>>>
>>> const auto x = g(obj); // different overload g for Foo and Bar
>>> // more code
>>> }
>>>
>>>
>>> What I really want to write is a ternary operator
>>> const auto obj = pred<T> ? makeFoo() : makeBar();
>>>
>>> But Foo and Bar are different types, so it won't work.
>>>
>>> What about allowing us to write
>>> const auto obj = pred<T> constexpr? makeFoo() : makeBar();
>>>
>>> Any ideas?
>>>
>>> Thanks
>>> Hui
>>>
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
that can be solved by macro.
#include <iostream>
#define CONSTEXPR_TERNARY(C, A, B) [&] { \
if constexpr (C) { return (A);} \
else { return (B); } \
}();
int main() {
auto x = CONSTEXPR_TERNARY(true, 5, "ABC");
std::cout << x << std::endl;
auto y = CONSTEXPR_TERNARY(false, 5, "ABC");
std::cout << y << std::endl;
}
https://godbolt.org/z/11911TT3c
On Thu, Oct 21, 2021 at 7:37 PM Hui Xie via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Yes, having more little functions is considered better practice. But
> the code still looks pretty much the same as my original code in terms of
> lines of code and structure of the code. I think even with a separated
> more_code function, having a ternary operator can make constexpr code more
> like the way we wrote time code
>
> template <typename T>
> void f(T const& t){
> const auto obj = pred<T> constexpr? makeFoo() : makeBar();
> more_code(obj);
> }
>
> On Thu, Oct 21, 2021 at 2:06 PM Nikolay Mihaylov <nmmm_at_[hidden]> wrote:
>
>> what about restructuring like this:
>>
>> template<typename T>
>> void more_code(T a);
>>
>> template <typename T>
>> void f(T const& t){
>> if constexpr (pred<T>){
>> return more_code(makeFoo());
>> } else {
>> return more_code(makeBar());
>> }
>> }
>>
>>
>> On Thu, Oct 21, 2021 at 3:49 PM Hui Xie via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> Hi,
>>>
>>> Sometimes I have this situation
>>>
>>> template <typename T>
>>> void f(T const& t){
>>> const auto obj = []{
>>> if constexpr (pred<T>){
>>> return makeFoo();
>>> } else {
>>> return makeBar();
>>> }
>>> }();
>>>
>>> const auto x = g(obj); // different overload g for Foo and Bar
>>> // more code
>>> }
>>>
>>>
>>> What I really want to write is a ternary operator
>>> const auto obj = pred<T> ? makeFoo() : makeBar();
>>>
>>> But Foo and Bar are different types, so it won't work.
>>>
>>> What about allowing us to write
>>> const auto obj = pred<T> constexpr? makeFoo() : makeBar();
>>>
>>> Any ideas?
>>>
>>> Thanks
>>> Hui
>>>
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
-- Mohit Saini
Received on 2021-10-21 09:45:10