View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Syntax I've never seen before

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

17 replies to this topic

### #1sweetRum  Members

Posted 18 September 2013 - 05:35 PM

I'm implementing a licensing api that has lib files written in C (which may have something to do with the exotic syntax). My application is written in C++ and I've never used C before. It looks like this:

License::License(int productID, const wstring version, etc)
{
}

can't make sense of the: L"" value.  Is there a guru out there that knows?

### #2SiCrane  Moderators

Posted 18 September 2013 - 05:41 PM

POPULAR

It marks the string literal as a wide character string literal, meaning that each element of the string is a wchar_t rather than a normal char.

### #3sweetRum  Members

Posted 18 September 2013 - 05:47 PM

i c, thank you

### #4sweetRum  Members

Posted 19 September 2013 - 02:43 PM

@SiCrane Is that C syntax?

### #5SiCrane  Moderators

Posted 19 September 2013 - 02:54 PM

Yes, it was added in the 1990 version of the C standard.

### #6sweetRum  Members

Posted 19 September 2013 - 03:01 PM

Does that mean that it is common to intermingle C syntax within C++ code?

### #7SiCrane  Moderators

Posted 19 September 2013 - 03:05 PM

It's also legal C++ syntax.

### #8sweetRum  Members

Posted 19 September 2013 - 03:10 PM

i C, thanks SiCrane.

### #9Servant of the Lord  Members

Posted 19 September 2013 - 03:10 PM

Though you know that that function is C++, and not C, because it uses std::wstring (the wide char version of std::string).

(It's also definitely a C++ piece of code, because it's in a class constructor).

L"" is C++ just as much as L"" is C, so here it wouldn't be considered "intermingling" C and C++, but just C++ code.

What I mean to say is, don't think of L"" as 'C'. It's fully C++ even if C also has that feature. C also has variables, but when you encounter variables in C++, you don't think of variables as "Oh, that's a C thing".

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #10sweetRum  Members

Posted 19 September 2013 - 03:48 PM

in that sence couldn't you say the same thing about C in its entirety since C++ is a subset of C?

Which leads to the question: if "wstring d_LicenseFilePath;" was declared as a private member var of the class, why establish the wstring literal type again in the constructor?

### #11frob  Moderators

Posted 19 September 2013 - 04:06 PM

in that sence couldn't you say the same thing about C in its entirety since C++ is a subset of C?

What kind of nonsense is that?

Very brief history lesson:

C had many dialects.  The original was written mostly by Dennis Ritchie at Bell labs from 1969-1973. The language was applied in several places and was customized and changed around.  In 1979 there was a book attempting to standardize the language, as K&R C.  The book was written by the two who contributed most to the language at that time.

The C++ language was one of the many dialects, called "C with Classes", used internally by AT&T. It was a dialect of the language.

Note that "C with Classes" was not a proper subset of K&R C. Even though they shared a common base and most of the trivial K&R C code could be made to compile with it, they were not absolutes.  You could write K&R C programs that would not complile in C with Classes, and you could write C with Classes code that would not compile in K&R C.

Even back then, in 1980, neither language was a subset of the other.

In 1983 when the C language began to be standardized by ANSI, the "C with Classes" version was renamed C++.  When C++ compilers hit the public in 1985 the language was a sibling of ANSI C, neither had a subset/superset or parent/child relationship.  They were similar but distinct.

The two languages have a similar root from the 1970's, but neither language has ever been a subset of the other.  They are similar, and many constructs are the same, but in no way is C a subset of C++, nor the other way around.  The two languages have always been separate and distinct.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #12sweetRum  Members

Posted 19 September 2013 - 04:27 PM

The two languages have always been separate and distinct.

ok, I was lead to believe that C++ was a direct derivation of C.

That threw me off as well as the idea that C and C++ get along so well and that I had never seen that syntax style before.

But really, why re-establish the data type within the constructor method when the data type was determined in the declaration?

### #13SiCrane  Moderators

Posted 19 September 2013 - 04:34 PM

Because the right hand side of an assignment needs to be a compatible type with the left hand side of an assignment. Just as you can't assign an int to a string, you can't assign a regular string literal to a wstring.

### #14sweetRum  Members

Posted 19 September 2013 - 04:47 PM

ok to summ it all up, please correct me if I am wrong.

wstring d_LicenseFilePath;

declares a wide string var...

L""

marks the string literal L as a wide character string literal and...

d_LicenseFilePath = L""

assigns the value L to "d_LicenseFilePath" var which contains only wide string literals

### #15Servant of the Lord  Members

Posted 19 September 2013 - 04:47 PM

in that sence couldn't you say the same thing about C in its entirety since C++ is a subset of C?

Even though alot of C syntax compiles properly in C++, some stuff isn't used in the common everyday C++ that is more common among C users. The stuff that, while functional in C++, isn't normally used (purely subjectively on my part), I tend to think of as more for 'C' backwards compatibility.

For example:

typedef struct MyStruct
{
//...
};

What is the 'typedef' there for? In C++, normally, unless you're being backwards compatible, you just type 'struct MyStruct'.

Again, subjective on my part. My only point is that L"" isn't there just for backwards compatibility with C, it's there because you need to be able to define wide-character literals in C++ during normal C++ usage (if you need wide characters).

"string literal" becomes const char*

L"string literal" becomes a const wchar_t*

Since they are different types, you can't (and shouldn't be able to, to avoid errors) assign a const char* to a std::wstring, so if you wanted to, within your code, assign a literal to a wstring (or to a wchar_t pointer), you'd need some way to create literals of that type, and that's what the L prefix is for.

In C++11, two more string types were defined:

std::u16string, which manages strings composed of char16_t*.

You can declare raw char16_t string literals like this: u"string literal"  (a const char16_t*)

std::u32string, which manages strings composed of char32_t*.

You can declare raw char32_t string literals like this: U"string literal"  (a const char32_t*)

Which leads to the question: if "wstring d_LicenseFilePath;" was declared as a private member var of the class, why establish the wstring literal type again in the constructor?

You're correct that 'd_LicenseFilePath' was already initialized to an empty string by default, so that redundant initialization isn't necessary.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #16Servant of the Lord  Members

Posted 19 September 2013 - 04:51 PM

ok to summ it all up, please correct me if I am wrong.

wstring d_LicenseFilePath;
declares a wide string var...

Yes, it defines a std::string-like class that takes wchar_t* instead of char*.
Both std::string and std::wstring are just typedefs of the template class std::basic_string.

L""
marks the string literal L as a wide character string literal and...

Yep.

d_LicenseFilePath = L""
assigns the value L to "d_LicenseFilePath" var which contains only wide string literals

The 'L' isn't anything except to tell the compiler that the following string literal is to be a const wchar_t* instead of const char*.
L"" is the same as "", except in wide characters.
L"Meow" is the same as "Meow", except in wide characters.

L"" is an empty wchar_t* string. The L isn't any part of the string itself.
It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #17sweetRum  Members

Posted 19 September 2013 - 04:56 PM

oh i c, thank you Servant of the Lord. In other words: A redundancy of no perceivable consequence:)

### #18sweetRum  Members

Posted 19 September 2013 - 05:01 PM

Thank you Servant of the Lord. I'm starting a Knights of St. John chapter. If you would like to join, just wish it so:)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.