Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verbatim Literals in Managed C++? (like C#'s @"blah")

Is there a way to use verbatim String literals in managed C++? Similar to C#'s

String Docs = @"c:\documents and settings\"
like image 490
Brian Avatar asked Dec 09 '08 16:12

Brian


People also ask

What is a verbatim literal?

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello".

What is verbatim string literal in C#?

In C#, a verbatim string is created using a special symbol @. @ is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string.

What type is a string literal in C?

In C the type of a string literal is a char[]. In C++, an ordinary string literal has type 'array of n const char'. For example, The type of the string literal "Hello" is "array of 6 const char". It can, however, be converted to a const char* by array-to-pointer conversion.


2 Answers

in C++11, there is raw string literal:

cout<<R"((\"ddd\aa)\n)"<<endl;
cout<<R"delimiter((\"ddd\aa)\n)delimiter"<<endl;

output is:

(\"ddd\aa)\n
(\"ddd\aa)\n
like image 61
Wang Avatar answered Sep 28 '22 04:09

Wang


This is not currently possible. Managed C++ string literals have almost the exact same rules as normal C++ strings. The managed C++ spec is in fact just an augmentation of the ANSI C++ standard.

Currently there is no support for C# style literal syntax in C++ (managed or not). You must manually escape every character.

See Section 9.1.3.3 in the C++/CLI spec for more details. (Spec Link)

like image 20
JaredPar Avatar answered Sep 28 '22 04:09

JaredPar