string-literals

Is the u8 string literal necessary in C++11

五迷三道 提交于 2019-11-29 02:36:12
问题 From Wikipedia: For the purpose of enhancing support for Unicode in C++ compilers, the definition of the type char has been modified to be at least the size necessary to store an eight-bit coding of UTF-8. I'm wondering what exactly this means for writing portable applications. Is there any difference between writing this const char[] str = "Test String"; or this? const char[] str = u8"Test String"; Is there be any reason not to use the latter for every string literal in your code? What

Possible to make custom string literal prefixes in Python?

混江龙づ霸主 提交于 2019-11-29 02:26:27
Let's say I have a custom class derived from str that implements/overrides some methods: class mystr(str): # just an example for a custom method: def something(self): return "anything" Now currently I have to manually create instances of mystr by passing it a string in the constructor: ms1 = mystr("my string") s = "another string" ms2 = mystr(s) This is not too bad, but it lead to the idea that it would be cool to use a custom string prefix similar to b'bytes string' or r'raw string' or u'unicode string' . Is it somehow possible in Python to create/register such a custom string literal prefix

auto with string literals

岁酱吖の 提交于 2019-11-29 01:19:46
#include <iostream> #include <typeinfo> int main() { const char a[] = "hello world"; const char * p = "hello world"; auto x = "hello world"; if (typeid(x) == typeid(a)) std::cout << "It's an array!\n"; else if (typeid(x) == typeid(p)) std::cout << "It's a pointer!\n"; // this is printed else std::cout << "It's Superman!\n"; } Why is x deduced to be a pointer when string literals are actually arrays? A narrow string literal has type "array of n const char " [2.14.5 String Literals [lex.string] §8] The feature auto is based on template argument deduction and template argument deduction behaves

String in function parameter

早过忘川 提交于 2019-11-28 23:41:20
int main() { char *x = "HelloWorld"; char y[] = "HelloWorld"; x[0] = 'Z'; //y[0] = 'M'; return 0; } In the above program, HelloWorld will be in read-only section(i.e string table). x will be pointing to that read-only section, so trying to modify that values will be undefined behavior. But y will be allocated in stack and HelloWorld will be copied to that memory. so modifying y will works fine. String literals: pointer vs. char array Here is my Question: In the following program, both char *arr and char arr[] causes segmentation fault if the content is modified. void function(char arr[]) /

How to make Jade stop HTML encoding element attributes, and produce a literal string value?

守給你的承諾、 提交于 2019-11-28 19:08:13
UPDATE Jade v0.24.0 fixes this with a != syntax for attributes. option(value!='<%= id %>') I'm trying to build an <option> with jade, where the value of the option is an UnderscoreJS template marker: <%= id %> but I can't get it to work because jade is converting my marker text to <= id > . Here's my Jade markup: script(id="my-template", type="text/template") select(id="type") <% _.each(deviceTypes, function(type){ %> option(value='<%= type.id %>') <%= type.name %> <% }) %> I expect it to produce this html: <script id="my-template" type="text/template"> <select id='type'> <% _.each(deviceTypes

what does cout << “\\n”[a==N]; do?

假如想象 提交于 2019-11-28 19:04:05
In the following example: cout<<"\n"[a==N]; I have no clue about what the [] option does in cout , but it does not print a newline when the value of a is equal to N . I have no clue about what the [] option does in cout This is actually not a cout option, what is happening is that "\n" is a string literal . A string literal has the type array of n const char , the [] is simply an index into an array of characters which in this case contains: \n\0 note \0 is appended to all string literals. The == operator results in either true or false , so the index will be: 0 if false, if a does not equal N

Comparing character arrays and string literals in C++

删除回忆录丶 提交于 2019-11-28 13:47:23
I have a character array and I'm trying to figure out if it matches a string literal, for example: char value[] = "yes"; if(value == "yes") { // code block } else { // code block } This resulted in the following error: comparison with string literal results in unspecified behavior. I also tried something like: char value[] = "yes"; if(strcmp(value, "yes")) { // code block } else { // code block } This didn't yield any compiler errors but it is not behaving as expected. std::strcmp returns 0 if strings are equal. Check the documentation for strcmp. Hint: it doesn't return a boolean value. ETA:

Isn't there a syntax error? Should printf(“one” “, two and ” “%s.\\n”, “three” ); be valid code?

折月煮酒 提交于 2019-11-28 12:56:16
Take a look at this code: #include <stdio.h> #define _ONE "one" #define _TWO_AND ", two and " int main() { const char THREE[6] = "three" ; printf(_ONE _TWO_AND "%s.\n", THREE ); return 0; } The printf is effectively: printf("one" ", two and " "%s.\n", "three" ); and the output is: one, two and three. gcc gives neither error nor warning messages after compiling this code. Is the gcc compiler supposed work in that way, or is it a bug? This is standard behavior, adjacent string literals are concatenated together if we look at the C99 draft standard section 5.1.1.2 Translation phases paragraph 6

Why does gcc allow char array initialization with string literal larger than array?

天涯浪子 提交于 2019-11-28 11:57:35
int main() { char a[7] = "Network"; return 0; } A string literal in C is terminated internally with a nul character . So, the above code should give a compilation error since the actual length of the string literal Network is 8 and it cannot fit in a char[7] array. However, gcc (even with -Wall ) on Ubuntu compiles this code without any error or warning. Why does gcc allow this and not flag it as compilation error? gcc only gives a warning (still no error!) when the char array size is smaller than the string literal. For example, it warns on: char a[6] = "Network"; [Related] Visual C++ 2012

Something like print END << END; in C++?

邮差的信 提交于 2019-11-28 10:04:54
Is there anyway to do something like PHP's print << END yadayadayada END; in C++? (multi-line, unescaped, easy-to-cut-and-paste stream insertion) RichieHindle This answer is now out of date for modern C++ - see sbi's answer for the modern way. This is the best you can do: std::cout << "This is a\n" "multiline\n" "string.\n"; Not as convenient as a proper heredoc, but not terrible. sbi C++11 has raw string literals : // this doesn't have '\n', but '\\' and 'n' R"(yada"yadayada\n)" And if you need those parens, you can do that, too, using whatever you want for an end token: // the following will