literals

Trying to pass string literals as template arguments [duplicate]

主宰稳场 提交于 2019-11-28 12:14:28
This question already has an answer here: Passing a string literal as a parameter to a C++ template class 12 answers I'm trying to find a comfortable way to pass string literals as template arguments. I'm not caring about supporting the widest possible number of compilers, I'm using the latest version of g++ with --std=c++0x . I've tried a lot of possible solutions but all have disappointed me. I'm sort of giving up, but first I'd like to know why a couple of them failed. Here they are: #include <iostream> #include <string> using namespace std; struct String { char const *m_sz; constexpr

Do RegExps made by expression literals share a single instance?

拜拜、爱过 提交于 2019-11-28 11:01:14
问题 The following snippet of code (from Crockford's Javascript: The Good Parts ) demonstrates that RegExp objects made by regular expression literals share a single instance: function make_a_matcher( ) { return /a/gi; } var x = make_a_matcher( ); var y = make_a_matcher( ); // Beware: x and y are the same object! x.lastIndex = 10; document.writeln(y.lastIndex); // 10 Question : Is this the same with any other literals? I tried modifying the code above to work with the string "string" , but got a

C++11 operator“” with double parameter

与世无争的帅哥 提交于 2019-11-28 10:42:32
Consider: struct str {}; str operator"" _X(long double d) { return str(); } This compiles fine with g++ 4.7.2 Wall std=c++11 but now if I give a double : str operator"" _X(double d) { return str(); } I get the following error message: main.cpp|3|error: 'str operator"" _X(double)' has invalid argument list What is the problem ? Has this something to do with "It is not possible to redefine the meaning of a built-in literal suffix" (Stroustrup FAQ) ? Can you think of a workaround ? What is the problem? The problem is that the Standard forbids it. Per paragraph 13.5.8./3 of the C++11 Standard on

python ValueError: invalid literal for float()

南楼画角 提交于 2019-11-28 09:41:54
I've a script which reads temperature data: def get_temp(socket, channels): data = {} for ch in channels: socket.sendall('KRDG? %s\n' % ch) time.sleep(0.2) temp = socket.recv(32).rstrip('\r\n') data[ch] = float(temp) Sometimes, the script fails on the line which converts the values to float: File "./projector.py", line 129, in get_temp data[ch] = float(temp) ValueError: invalid literal for float(): +135.057E+0 +078.260E+0 +00029 but this is NOT an invalid literal. If I enter this into any python shell, float(+135.057E+0) then it correctly returns 135.057. So what is the problem? I would all

How does one escape characters in Delphi string

主宰稳场 提交于 2019-11-28 09:35:31
Delphi strings use single quotes, for example ' a valid string '. How does one specify the ' character within a literal string? How would one refer to the null byte (Unicode code point U+0000 )? Jamie To add a single quote to a string, you include two ' marks e.g. str := '''test string'''; Writeln(str) In the string above, you have the normal single quotation to start a string and then two for the single quote. Same goes for the end of the string. You can also use # followed by a number for other escape character e.g. For a new line: str := 'Newline' + #13 + #10 or just str := 'Newline'#13#10

Why do Haskell numerical literals need to start and end with digits?

一个人想着一个人 提交于 2019-11-28 09:20:22
问题 In The Haskell 98 Report it's said that A floating literal must contain digits both before and after the decimal point; this ensures that a decimal point cannot be mistaken for another use of the dot character. What other use might this be? I can't imagine any such legal expression. (To clarify the motivation: I'm aware that many people write numbers like 9.0 or 0.7 all the time without needing to, but I can't quite befriend myself with this. I'm ok with 0.7 rather then the more compact but

Function literal in PHP class

一曲冷凌霜 提交于 2019-11-28 09:07:40
问题 Take a look at this code, please: $array = array( 'action' => function () { echo "this works"; } ); class Test { public $array = array( "action" => function () { echo "this doesn't"; } ); } The first function literal parses fine, but the second - the one inside the class - triggers a syntax error: Parse error: syntax error, unexpected 'function' (T_FUNCTION)... Can somebody explain this to me? Is this a bug? EDIT: This is the latest PHP: 5.6.6 回答1: From the class it's a property ! Rule from

How do I include extremely long literals in C++ source?

不打扰是莪最后的温柔 提交于 2019-11-28 08:05:20
问题 I've got a bit of a problem. Essentially, I need to store a large list of whitelisted entries inside my program, and I'd like to include such a list directly -- I don't want to have to distribute other libraries and such, and I don't want to embed the strings into a Win32 resource, for a bunch of reasons I don't want to go into right now. I simply included my big whitelist in my .cpp file, and was presented with this error: 1>ServicesWhitelist.cpp(2807): fatal error C1091: compiler limit:

Assign a string literal to a char* [duplicate]

雨燕双飞 提交于 2019-11-28 07:08:23
问题 Possible Duplicate: How to get rid of deprecated conversion from string constant to ‘char*’ warnings in GCC? This assignment: char *pc1 = "test string"; gives me this warning: warning: deprecated conversion from string constant to 'char*' while this one seems to be fine: char *pc2 = (char*)("test string"); Is this one a really better way to proceed? Notes: for other reasons I cannot use a const char* . 回答1: A string literal is a const char[] in C++, and may be stored in read-only memory so

Is order of a Ruby hash literal guaranteed?

こ雲淡風輕ζ 提交于 2019-11-28 06:57:11
问题 Ruby, since v1.9, supports a deterministic order when looping through a hash; entries added first will be returned first. Does this apply to literals, i.e. will { a: 1, b: 2 } always yield a before b? I did a quick experiment with Ruby 2.1 (MRI) and it was in fact consistent, but to what extent is this guaranteed by the language to work on all Ruby implementations? 回答1: There are couple of locations where this could be specified, i.e. a couple of things that are considered "The Ruby Language