Initialize a c-string in C++11? [duplicate] - c++

This question already has answers here:
Conversion from string literal to char* is deprecated [duplicate]
(2 answers)
Closed 7 years ago.
How do I initialize a c-string in C++11?
I tried:
char* foo = {"bar"};
but I get:
ISO C++11 does not allow conversion from string literal to 'char *

Because it has to be pointer to const:
const char* foo = "bar";
Before C++11, you could make foo simply char*, even though that was deprecated since C++03. But C++11 removes the deprecation and simply makes it illegal.

The correct way to do it is;
char const* foo = {"bar"};
// ^^^^^ added const
The older C style (non const) was deprecated and now is removed from C++11.

Related

Change a C library function to get rid of implicit const cast warnings from g++ [duplicate]

This question already has answers here:
Why is conversion from string constant to 'char*' valid in C but invalid in C++
(4 answers)
Closed 3 years ago.
very simple code does warn me. Some hints are not constructive. Warning is:
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
I tried:
char const *q = "pin";
char const *r = "\n\r";
{
while(client.findUntil(*q, *r))
without success
Origin code:
while(client.findUntil("pin", "\n\r"))
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
while(client.findUntil("pin", "\n\r"))
The warning means that your program is ill-formed. You didn't show the declaration, but from context, we can deduce that the argument of findUntil is char*. You may not pass a string literal to such function.
It used to be well-formed - but deprecated - to pass a string literal as char* prior to C++11.
I tried:
char const *q = "pin";
char const *r = "\n\r";
These are correct by themselves, but
while(client.findUntil(*q, *r))
This makes no sense. Previously your were attempting to pass a string, but now you indirect through the character pointer so you are passing a character. Unless the function is a template, this cannot possibly work.
findUntil(q, r) won't work either, because the pointers to const won't implicitly convert to pointers to non-const.
A correct solution is to copy the string literals into modifiable arrays:
char q[] = "pin";
char r[] = "\n\r";
{
while(client.findUntil(q, r))
Another is to fix findUntil to accept a pointer to const char instead. Then you can use string literals, since they can be converted to a pointer to const char.

c++ warning C4114 same type qualifier used more than once [duplicate]

This question already has answers here:
warning C4114: same type qualifier used more than once
(2 answers)
Closed 2 years ago.
I got a warning: same type qualifier used more than once
with these prototype:
static std::wstring joinWS(const vector<wchar_t*> Vect, const const char* delimiter);
static std::wstring joinWS(const vector<char*> Vect, const const char* delimiter);
wchar_t* and char* are different.
There is a way to fix it ?
Thanks you !
const is the type qualifier, and you should not use it twice in the same place like that.
"It’s like, how much more const could this be? And the answer is none.
None more const."
-- Bjarne Stroustrup

c++ the difference between string using the assign function and directly using '=' to change the value [duplicate]

This question already has answers here:
std::string::assign vs std::string::operator=
(2 answers)
Closed 2 years ago.
For example, this code
std::string a("this is a string");
std::string b;
b = a;
std::string c;
c.assign(a);
Is there any difference between B and C in essence?
From cppreference
2) basic_string& assign( const basic_string& str );
...
2) Replaces the contents with a copy of str. Equivalent to *this = str;. In particular, allocator propagation may take place. (since C++11)
So this does the same.

Can't (implicitly) convert from char** to const char**? [duplicate]

This question already has answers here:
Cannot convert from XXX** to const XXX** [duplicate]
(3 answers)
How to convert "pointer to pointer type" to const?
(2 answers)
Closed 4 years ago.
E.g. the following will fail to compile:
auto foo = [](const char**) {};
char* ptr = nullptr;
foo(&ptr);
Visual studio says that there's a conversion that loses qualifiers. Clang says cannot initialize a parameter of type 'const char **' with an rvalue of type 'char **". Perhaps my brain just isn't working today, but why is this dis-allowed? char* can implicitly convert to const char* after all
Oh wait, yes my brain isn't working today. foo can assign a const char* to the pointer, which is clearly bad.

C++ implicit conversion: Why should literal string convert to string? [duplicate]

This question already has answers here:
C++ implicit conversions
(4 answers)
Closed 7 years ago.
The example is from Chapter 7 in C++ Primer 5th.
Suppose class Sales_data has such constructor:
Sales_data(const std::string &s): bookNo(s) { }
And it has a public function member:
Sales_data &combine(Sales_data &s){...}
The flowing is error:(item is a Sales_data instance)
item.combine("9-999-9999");
The reason is that: Only One Class-Type Conversion Is Allowed, however, the code mentioned above has two user-defined conversions.
"9-999-9999" to string
string to Sales_data
Why should literal string convert to string ? Is 9-999-9999 not a string ?
"9-999-9999" is not a string, it is a const char[]. You can fix this by adding a constructor to you class that takes a const char * like:
Sales_data(const char* s): bookNo(s) { };
If you have C++14 support than you could also use a std::string_literal:
item.combine("9-999-9999"s);