string-literals

g++ treats returned string literal as const char pointer not const char array

旧巷老猫 提交于 2019-11-28 09:12:00
I'm seeing some odd behaviour when returning a string literal from a function that should perform an implicit conversion with g++ (version 4.7.3). Can anyone explain why the following code: #include <stdio.h> class Test { public: template <unsigned int N> Test(const char (&foo)[N]) { printf("Template const char array constructor\n"); } Test(char* foo) { printf("char* constructor\n"); } }; Test fn() { return "foo"; } int main() { Test t("bar"); Test u = fn(); return 0; } produces the result: Template const char array constructor char* constructor on g++? The surprising thing being that the char

Searching For String Literals

跟風遠走 提交于 2019-11-28 09:04:53
In the quest for localization I need to find all the string literals littered amongst our source code. I was looking for a way to script this into a post-modification source repository check. (I.E. after some one checks something in have a box setup to check this stat) I'll probably use NAnt and CruiseControl or something to handle the management of the CVS (Well StarTeam in my case :( ) But do you know of any scriptable (or command line) utility to accurately cycle through source code looking for string literals? I realize I could do simple string look up based on regular expressions but want

What does assigning a literal string to an NSString with “=” actually do?

眉间皱痕 提交于 2019-11-28 07:30:12
问题 What does the following line actually do? string = @"Some text"; Assuming that "string" is declared thusly in the header: NSString *string; What does the "=" actually do here? What does it do to "string"'s reference count? In particular, assuming that for some reason "string" is not otherwise assigned to, does it need to be released? Thanks! 回答1: The assignment is just that. The string pointer is basically a label that points to specific address in memory. Reassignment statement would point

Difference between string literal and constexpr array of char

大憨熊 提交于 2019-11-28 07:11:05
问题 I have been wondering if there is any difference between what is being pointed by ptrToArray and ptrToLiteral in the following example: constexpr char constExprArray[] = "hello"; const char* ptrToArray = constExprArray; const char* ptrToLiteral = "hello"; Is my understanding that constExprArray and the two "hello" literals are all compile time constant lvalues correct? If so, is there any difference in how they are stored in the executable file, or is it purely compiler implementation or

Is there a way to pull in a text resource into a raw string literal using the pre-processor?

浪子不回头ぞ 提交于 2019-11-28 04:47:33
问题 I've just noticed that an answer I have given for this question actually doesn't work: Regardless of using CMake or not, the following should work with the current standard: std::string resource = R"( #include "text.txt" )"; I thought that the pre-processor would recognize the #include "text.txt" statement in first place and expand the text. But that's obviously not the case, the result for std::cout << resource << std::endl; is #include "text.txt" I tried to use some macro to let the

Same strings in array have same memory address

给你一囗甜甜゛ 提交于 2019-11-28 04:43:43
问题 Why do same strings in a char* array have the same address? Is this because of compiler optimization? Example: #include <stdio.h> #include <stdlib.h> #include <string.h> #define ARR_SIZE 7 int main(int argc, char** argv) { size_t i = 0, j = 0; char * myArr[ARR_SIZE] = { "This is the first string", "This is the second string", "This is Engie", "This is the third string", "This is Engie", "This is the fifth string", "This is Engie" }; for (i = 0; i < ARR_SIZE; ++i){ for (j = i + 1; j < ARR_SIZE

Avoid warning in wrapper around printf

巧了我就是萌 提交于 2019-11-28 03:53:50
问题 I have an error reporting functionality in my little C library I'm writing. I want to provide an errorf function in addition to the plain error function to allow embedding information in error messages easily. /* * Prints a formatted error message. Use it as you would use 'printf'. See the * 'sio_error' function. */ void sio_errorf(const char *format, ...) { // Print the error prefix if (g_input == STDIN) fputs("error: ", stderr); else fprintf(stderr, "%s: ", g_progname); // Pass on the

What is the rationale for parenthesis in C++11's raw string literals R“(…)”?

空扰寡人 提交于 2019-11-28 03:45:35
There is a very convenient feature introduced in C++11 called raw string literals, which are strings with no escape characters. And instead of writing this: regex mask("\\t[0-9]+\\.[0-9]+\\t\\\\SUB"); You can simply write this: regex mask(R"(\t[0-9]+\.[0-9]+\t\\SUB)"); Quite more readable. However, note extra parenthesis around the string one have to place to define a raw string literal. My question is, why do we even need these? For me it looks quite ugly and illogical. Here are the cons what I see: Extra verbosity, while the whole feature is used to make literals more compact Hard to

Assigning string literals to char*

僤鯓⒐⒋嵵緔 提交于 2019-11-28 01:15:39
Is the following code legal, deprecated or illegal in C++0x? char* p = "foobar"; I originally asked this question here as a comment. The conversion char* p = "foobar"; is deprecated in C++98/C++03, and has been removed (that is, §4.2/2 removed) in C++0x. So, the code is not valid in C++0x. However, MinGW g++ 4.4.1 still only emits a warning, not error. C++98/C++03 §4.2/2 (which is removed in C++0x): A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char ”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t

Properly match a Java string literal [duplicate]

一世执手 提交于 2019-11-28 00:41:42
问题 This question already has an answer here: Regex to replace all string literals in a Java file 4 answers I am looking for a Regular expression to match string literals in Java source code. Is it possible? private String Foo = "A potato"; private String Bar = "A \"car\""; My intent is to replace all strings within another string with something else. Using: String A = "I went to the store to buy a \"coke\""; String B = A.replaceAll(REGEX,"Pepsi"); Something like this. 回答1: Ok. So what you want