case-statement

Is There Anything Like a Templatized Case-Statement

半城伤御伤魂 提交于 2019-11-27 23:47:29
问题 So I have this really ugly code: template <typename T> std::conditional_t<sizeof(T) == sizeof(char), char, conditional_t<sizeof(T) == sizeof(short), short, conditional_t<sizeof(T) == sizeof(long), long, enable_if_t<sizeof(T) == sizeof(long long), long long>>>> foo(T bar){return reinterpret_cast<decltype(foo(bar))>(bar);} I'm using nested conditional_t s to make a case-statement of sorts. Is there something out there that accomplishes this more elegantly or do I need to cook up my own

Why is CharInSet faster than Case statement?

时光怂恿深爱的人放手 提交于 2019-11-27 20:10:17
I'm perplexed. At CodeRage today, Marco Cantu said that CharInSet was slow and I should try a Case statement instead. I did so in my parser and then checked with AQTime what the speedup was. I found the Case statement to be much slower. 4,894,539 executions of: while not CharInSet (P^, [' ', #10,#13, #0]) do inc(P); was timed at 0.25 seconds. But the same number of executions of: while True do case P^ of ' ', #10, #13, #0: break; else inc(P); end; takes .16 seconds for the "while True", .80 seconds for the first case, and .13 seconds for the else case, totaling 1.09 seconds, or over 4 times as

Ruby class types and case statements

只愿长相守 提交于 2019-11-27 10:53:59
What is the difference between case item.class when MyClass # do something here when Array # do something different here when String # do a third thing end and case item.class when MyClass.class # do something here when Array.class # do something different here when String.class # do a third thing end For some reason, the first one of these works sometimes and the second doesn't, and other times, the second one works and the first one doesn't. Why? Which one is the "proper" way to do it? Nakilon You must use: case item when MyClass ... I had the same problem: How to catch Errno::ECONNRESET

Why is CharInSet faster than Case statement?

不羁的心 提交于 2019-11-27 04:25:26
问题 I'm perplexed. At CodeRage today, Marco Cantu said that CharInSet was slow and I should try a Case statement instead. I did so in my parser and then checked with AQTime what the speedup was. I found the Case statement to be much slower. 4,894,539 executions of: while not CharInSet (P^, [' ', #10,#13, #0]) do inc(P); was timed at 0.25 seconds. But the same number of executions of: while True do case P^ of ' ', #10, #13, #0: break; else inc(P); end; takes .16 seconds for the "while True", .80

Ruby class types and case statements

て烟熏妆下的殇ゞ 提交于 2019-11-27 04:02:16
问题 What is the difference between case item.class when MyClass # do something here when Array # do something different here when String # do a third thing end and case item.class when MyClass.class # do something here when Array.class # do something different here when String.class # do a third thing end For some reason, the first one of these works sometimes and the second doesn't, and other times, the second one works and the first one doesn't. Why? Which one is the "proper" way to do it? 回答1:

What is the Python equivalent for a case/switch statement? [duplicate]

孤人 提交于 2019-11-26 05:47:52
This question already has an answer here: Replacements for switch statement in Python? 44 answers I'd like to know, is there a Python equivalent for the case statement such as the examples available on VB.net or C#? While the official docs are happy not to provide switch, I have seen a solution using dictionaries . For example: # define the function blocks def zero(): print "You typed zero.\n" def sqr(): print "n is a perfect square\n" def even(): print "n is an even number\n" def prime(): print "n is a prime number\n" # map the inputs to the function blocks options = {0 : zero, 1 : sqr, 4 :

What is the Python equivalent for a case/switch statement? [duplicate]

蹲街弑〆低调 提交于 2019-11-26 01:04:06
问题 This question already has answers here : Replacements for switch statement in Python? (44 answers) Closed 6 years ago . I\'d like to know, is there a Python equivalent for the case statement such as the examples available on VB.net or C#? 回答1: While the official docs are happy not to provide switch, I have seen a solution using dictionaries. For example: # define the function blocks def zero(): print "You typed zero.\n" def sqr(): print "n is a perfect square\n" def even(): print "n is an