Post by Barry RevzinFor your first example, is this really better than
https://www.reddit.com/r/cpp/comments/7qcgne/rfc_proposal_ternary_fold_expression/dso41rx/?st=jcpl1gmq&sh=9ee1b9c4
?
Clang can optimize the nested conditional operators at best.
Godbolt comparison conditional vs. function pointers (clang)
<https://gcc.godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAKxAEZSAbAQwDtRkBSAJgCFufSAZ1QBXYskwgA5NwDMeFsgYisAag6yAwskEF0WAGYbsHAAwBBM%2BYKYAtgAdmN9Vt3oQIQXgBemAPoEqnjGVm4eXr4BqgYQAJRWHADsfBaqaarEmARiLEEAVMGyKZaJACIJFmGePv6BNroQVRG1qlTxFknF6RlZOarULuWypaqmsS4AYtEamqbGcVbdSyD9VIMaI7TjGlNGWrTz26lL6SsDGxuqXNuyuzNch%2BrHJ6pnaxfDqrI3d1qyj4sXq9VutPgAWH7TLRggHPE5vUEjACskL2miRsPMQOB2LSaIAbIcNDx0gkylYpLFGNIkVJSCxpKY6ahpJp%2BPxVMIxBJ1FxZLQ6QRGZSqQBrEBcfEAOi4iTBpkS%2BMltFkSMlAE5EtSpGC6bY6KZTPThaQWVI6YIQEahVImVS4LAkGgHHgGJgyBQIM77K73ShmGwkYbSAZXTZiJaIAAjE1RhRMYgAT2kAtIztsmBYBAA8iwGMnbXSsLZWMA3Sb8JlkAQ8AA3TCWwukTAAD0wyBENhTdIUNgYJoIxDw%2BsLVID7HZvEYeCjlsgVNQ9hrqBYjYAtNnZGk12u3BtOLx%2BLRU1zxJIttracam2aWwAOfFr/Fg1RKUuqJFS0xf/q4QgkXl%2BVIVRNFQF03WIQCthAyceEFYVYjFCVpXVdU%2BVMWh5S4Wg71QlVtV1Uh9VoYMGRvaQLStUgbTtUgHUQFAMBwfAiA9SgaC4RhSxQWChFEM9pDkBQlBUTAXG0NxDBCDoLBsBwnDEmYmhqKJChMSo9HCFTAhidoSi6dJMmyYhcjwApiTJcoZPMZTIjqBsCEaTTqjs1o9M6QE0hELw2GiJdIMuZoAggPJYgWIpPM5AgmBrZBXxXXRW3sSCDH81RoqjN0OCRPgkRGS5OjROZZGwYC0QOEqyvuYwqr%2BGqoU0GFKoajESqSIYDLSPADFWEJhnxPSgSMvoCSJCK4WGkz0qYTLMGyngqGy0pwuKdqKTHGk6TIplTVZWDOX4nk5C4eDRypBBMCYLBiEoJC%2BSldUwT5ME71kXDaGw0xJQIraTTNSjrQQuiYAY71fTYr0wJ9CD/VLIMjVDBhw0jGMmzjFgEwLVN00zHM8wLHbi1Lcsm0rdsa3rRsdtbdtO0kKRU17TB%2BybQdhyB8ceMPKcGBnOc4lNJc8AS6QNy3Hc92GA8%2BF4Y8LUOyRrkvX7yKke9H2fV9xw/L8fwgP9WMAziQKh8GjfGNlubg6iEPOy7rtu0hxS4LgpUSdUPsSWRHq92R8T9n7rx2/6hComiRW1E6g%2BZCibbO0h6wjYWGTBIA%3D%3D>
It transforms them to assembler code equivalent to a switch statement and
can inline the called function f<i>().
In the code with the array of function pointers functions f<i>() are not
inlined.
However, the proposal is not about how compilers may optimize the code or
not (since this might change in the future). The proposal contains a
natural extension of the fold expression on the conditional ternary
operator and shows example to illustrate the usage.
Additional, to be natural extension, I think that the conditional ternary
fold is easier to write than creating function pointers.
Post by Barry RevzinFor your second example, is this really better than having a hashtable
from std::string to std::function<std::string(std::string const&)> and just
doing a lookup + call?
If you want to make use of hashes you could do this with the fold
expression, too.
template <typename String>
constexpr std::size_t simple_hash(String const & str)
{
std::size_t ret = 0;
for(auto const & c : str) {
ret += c;
}
return ret;
}
template<class... translators>
std::string translate_to_english_impl(
std::string_view language,
std::string_view text)
{
auto lang_hash = hash(language);
return ( ( lang_hash == simple_hash(translators::language)
&& language == translators::language)
? translators::translate_to_english(text)
: ... : throw_unknown_language<std::string>(language) );
}
If you are sure to have no hash collisions you even could omit the
comparison of the language string, i.e.
template<class... translators>
std::string translate_to_english_impl(
std::string_view language,
std::string_view text)
{
auto lang_hash = hash(language);
return ( lang_hash == simple_hash(translators::language)
? translators::translate_to_english(text)
: ... : throw_unknown_language<std::string>(language) );
}
Post by Barry RevzinAlso, it's called the conditional operator, it's not called the ternary
operator (it simply is a ternary operator).
Since the conditional operator is the only ternary operator which exists in
C++ it is often used as synonym.
However, the ternary fold applies to any ternary operator like the
conditional operator ? :
as the binary fold applies to many binary operators, i.e. +, *, etc.
However, I did not want to write the proposal with an anonymous ternary
operator op_left op_right since it makes the proposal harder to read.
For an anonymous ternary operator op_left op_right the proposal would read:
( E_left op_left E_middle op_right ... op_right E_right)
would expand to
( E_left_1 op_left E_middle _op_right ( ... ( E_left_n op_left E_middle_n
op_right E_right ) ) )
etc.
I tried to write conditional in the proposal at any place where it is
concrete to the conditional operator.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/6bcbceb5-06d8-4da3-97e1-11951f55bc99%40isocpp.org.