literals

why must use const reference when reference to literals

感情迁移 提交于 2019-12-04 04:05:44
问题 I know only object could have reference. But literals are not object. So I could understand why following code cannot compile: int &a = '4'; int &b = 2; However, when I add const in front of them, it could work!! const int &a = '4'; const int &b = 2; I do not know why. Could anyone help me? 回答1: A integer or character literal is a prvalue [expr.prim.general] A literal is a primary expression. Its type depends on its form (2.13). A string literal is an lvalue; all other literals are prvalues.

How exactly does the JavaScript expression [1 [{}]] parse?

痴心易碎 提交于 2019-12-03 14:46:18
Can you explain how the JavaScript expression: [1 [{}]] parses/evaluates? In Firefox, Chrome, Konqueror, and rhino, it seems to create an array with a single element, undefined . However, I don't understand why. In Firefox: [1 [{}]].toSource() produces [(void 0)] Replacing 1 with other JavaScript values seems to yield the same result. Update: I think I understand now. codeka, Adrian, and CMS clarified things. As far as the standard, I tried to walk through ECMAScript 5. 1 [{}] is a Property Accessor, so it's covered in §11.2.1. baseReference is the result of evaluating 1 , so still 1 .

c++ passing a string literal instead of a const std::string&?

可紊 提交于 2019-12-03 13:38:12
I have the following code which compiles with no warnings (-Wall -pedantic) with g++ #include <iostream> #include <string> using namespace std; class Foo { public: Foo(const std::string& s) : str(s) { } void print() { cout << str << endl; } private: const std::string& str; }; class Bar { public: void stuff() { Foo o("werd"); o.print(); } }; int main(int argc, char **argv) { Bar b; b.stuff(); return 0; } But when I run it, only the newline is printed out. What is going on? If I were to do this inside stuff: string temp("snoop"); Foo f(temp); f.print(); then it works fine! The reason why this

Why is this string key in a hash converted to a symbol?

▼魔方 西西 提交于 2019-12-03 07:27:57
问题 Using Ruby 2.3: In example 1, the string key "a" is automatically converted to a symbol, whereas with example 2, it stays a string. Example 1 {"a": 1} # => {:a=>1} Example 2 {"a"=>"c"} # => {"a"=>"c"} I thought : was the same as the old style hash rocket => syntax. What is going on? Why have I never noticed this in Rails? Is it the HashWithIndifferentAccess that is obscuring this? 回答1: In Ruby 2.3(.0), these are all the same: {:"a" => 1} {"a": 1}, {:a => 1} {a: 1} They all translate to the

In C# are the terms “Primitive” and “Literal” interchangeable?

試著忘記壹切 提交于 2019-12-03 05:53:29
A discussion earlier today led me to question whether or not my understanding of primtives and literals is correct. My understanding is that a literal type is specifically a type which can have a value assigned using a notation that both human and compiler can understand without specific type declarations: var firstName = "John"; // "John" is literal var firstName = (string)"John"; // *if* the compiler didn't understand that "John" // was a literal representation of a string then I // would have to direct it as such My understanding of primitives is that they are essentially the elemental

JavaScript: JSLint throws \"Read Only

跟風遠走 提交于 2019-12-03 01:19:07
My code: note: the Slider Object is declared but omitted in the snippet below for better readability "use strict"; /*global arrayContainer, SliderInstance, DomObjects */ arrayContainer = new Slider.constructArray(); SliderInstance = Object.beget(Slider); DomObjects = { animationContainer: document.getElementById('animationContainer'), buttonRight: document.getElementById('buttonRight'), buttonRightDots: document.getElementById('buttonRightDots'), ieEffectImg: document.getElementById('ie_effectIMG') }; This is what JSLint produces (and on the other two Objects SliderInstance and DomObjects)

How to declare an array inline in VB.NET

匆匆过客 提交于 2019-12-03 00:54:00
I am looking for the VB.NET equivalent of var strings = new string[] {"abc", "def", "ghi"}; gfrizzle Dim strings() As String = {"abc", "def", "ghi"} There are plenty of correct answers to this already now, but here's a "teach a guy to fish" version. First create a tiny console app in C#: class Test { static void Main() { var strings = new string[] {"abc", "def", "ghi"}; } } Compile it, keeping debug information: csc /debug+ Test.cs Run Reflector on it, and open up the Main method - then decompile to VB. You end up with: Private Shared Sub Main() Dim strings As String() = New String() { "abc",

How do you declare a Char literal in Visual Basic .NET?

不羁的心 提交于 2019-12-03 00:52:32
With Option Strict On : Dim theLetterA As Char = "A" returns an error about converting the string "A" to a Char . What is the syntax to enter a Char literal? A character literal is entered using a single character string suffixed with a C . Dim theLetterA As Char = "A"C I would use CChar. E.g.: Dim theLetterA As Char = CChar("A") Check the MSDN website https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx for details on CChar. In the case of trying to get a double quote as a character literal, you'll need to use the extra quirky VB format: Dim theQuote As Char = """"C Or Dim theQuote As Char

What is the >>>= operator in C?

穿精又带淫゛_ 提交于 2019-12-03 00:01:58
问题 Given by a colleague as a puzzle, I cannot figure out how this C program actually compiles and runs. What is this >>>= operator and the strange 1P1 literal? I have tested in Clang and GCC. There are no warnings and the output is "???" #include <stdio.h> int main() { int a[2]={ 10, 1 }; while( a[ 0xFULL?'\0':-1:>>>=a<:!!0X.1P1 ] ) printf("?"); return 0; } 回答1: The line: while( a[ 0xFULL?'\0':-1:>>>=a<:!!0X.1P1 ] ) contains the digraphs :> and <: , which translate to ] and [ respectively, so it

Why is this string key in a hash converted to a symbol?

℡╲_俬逩灬. 提交于 2019-12-02 20:09:14
Using Ruby 2.3: In example 1, the string key "a" is automatically converted to a symbol, whereas with example 2, it stays a string. Example 1 {"a": 1} # => {:a=>1} Example 2 {"a"=>"c"} # => {"a"=>"c"} I thought : was the same as the old style hash rocket => syntax. What is going on? Why have I never noticed this in Rails? Is it the HashWithIndifferentAccess that is obscuring this? In Ruby 2.3(.0), these are all the same: {:"a" => 1} {"a": 1}, {:a => 1} {a: 1} They all translate to the same thing: a is a symbol in all these cases. {"a"=>1} is different: a is a string in this case. It's because