declaration

Why is “reference to array” defined in such confusing way in C++?

扶醉桌前 提交于 2019-12-10 21:38:15
问题 The following code is based on a snippet from here. I want a templated function that accepts a reference to an array of size that is deduced when the function is instantiated: template<int size> void myFunction( SomeType(&param)[size] ) { //use param and size here } //called like this: SomeType array[SomeConstant]; myFunction( array ); //SomeConstant magically gets into the function as "size" Now I'm confused with SomeType(&param)[size] . I'd expect the following to work: template<int size>

declaring a variable inside of if/else construct

ぐ巨炮叔叔 提交于 2019-12-10 18:58:23
问题 I am solving a basic Java string problem on CodingBat.com (extraFront). The task is to, given a string of any length, return the two first characters repeated three times. This first example is what I ended up doing intuitively: public String extraFront(String str) { if (str.length() <= 2){ String front = str; }else{ String front = str.substring(0,2); } return front+front+front; } Which gives me an error, front can not be resolved . I guessed that I needed to define the variable outside the

Declaring a member variable that takes a constructor parameter

亡梦爱人 提交于 2019-12-10 17:44:41
问题 // In A.h class A { public: enum eMyEnum{ eOne, eTwo, eThree }; public: A(eMyEnum e); } // In B.h #include "A.h" class B { B(); private: A memberA; } // In B.cpp #include "B.h" B::B(void) : memberA(A::eOne) {} The declaration to 'memberA' gives me a compile error using the g++ compiler: error: 'A::eOne' is not a type How can I overcome this? Do I simply need to create a default constructor that takes no parameters? 回答1: It sounds like you are trying to initialise a member variable. You could

Is C++ “declaration and initialization” statement, an expression?

帅比萌擦擦* 提交于 2019-12-10 14:43:17
问题 The language standard says: [ Note: Clause 5 defines the syntax, order of evaluation, and meaning of expressions.58 An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects. — end note ] E.g. I've code below: int i=1; A obj; So, do both statements above, count as "expression"? Some people on stackoverflow says "int i=1;" is not an expression. This is quite odd to me. (1) An initialization is a kind of

Why is there not any warning on a declaration without initialization in a for loop?

情到浓时终转凉″ 提交于 2019-12-10 13:56:53
问题 I tried to compile the following code using g++ (gcc version 4.8.2 (Debian 4.8.2-1)), with -Wall flag (adding the -Wextra flag does not change anything for me). #include <iostream> using namespace std ; int main() { int i ; cout << i << endl ; } It gave this warning: test.cpp: In function ‘int main()’: test.cpp:7:13: warning: ‘i’ is used uninitialized in this function [-Wuninitialized] cout << i << endl ; But the following code does not yield any warning: #include <iostream> using namespace

Proper organization for a mixture of templated and non-templated functions not accessible to the end-user

戏子无情 提交于 2019-12-10 13:46:26
问题 In a project I am working on, I am currently implementing an algorithm which is templated. I had some problems organizing my function declarations and definitions, because templates are involved (I asked a question here about how to deal with non-templated functions "grouped" together with the templated ones). This, however, made me wonder about a general proper organization of such files. I wanted to separate all the definitions from all the declarations (just for readability purposes, even

explicit qualification in C++ declaration

坚强是说给别人听的谎言 提交于 2019-12-10 13:21:40
问题 The following namespace definition fails to compile when the first declaration is commented out. If the first declaration of foo is uncommented, then it compiles just fine. namespace Y { //void foo(); void ::Y::foo(){} } The relevant part in the standard (§8.3¶1) says: When the declarator-id is qualified, the declaration shall refer to a previously declared member I understand that this rule prevents the introduction of names into other namespaces. I wonder if that rule could be relaxed to

Why does this Perl variable keep its value

ⅰ亾dé卋堺 提交于 2019-12-10 10:44:56
问题 What is the difference between the following two Perl variable declarations? my $foo = 'bar' if 0; my $baz; $baz = 'qux' if 0; The difference is significant when these appear at the top of a loop. For example: use warnings; use strict; foreach my $n (0,1){ my $foo = 'bar' if 0; print defined $foo ? "defined\n" : "undefined\n"; $foo = 'bar'; print defined $foo ? "defined\n" : "undefined\n"; } print "==\n"; foreach my $m (0,1){ my $baz; $baz = 'qux' if 0; print defined $baz ? "defined\n" :

Does redeclaring variables in C++ cost anything?

≯℡__Kan透↙ 提交于 2019-12-10 03:39:39
问题 For readability, I think the first code block below is better. But is the second code block faster? First Block: for (int i = 0; i < 5000; i++){ int number = rand() % 10000 + 1; string fizzBuzz = GetStringFromFizzBuzzLogic(number); } Second Block: int number; string fizzBuzz; for (int i = 0; i < 5000; i++){ number = rand() % 10000 + 1; fizzBuzz = GetStringFromFizzBuzzLogic(number); } Does redeclaring variables in C++ cost anything? 回答1: I benchmarked this particular code, and even WITHOUT

Detecting declared package variables in perl

。_饼干妹妹 提交于 2019-12-10 02:46:58
问题 Given # package main; our $f; sub f{} sub g {} 1; How can I determine that $f , but not $g , has been declared? Off the cuff, I'd thought that *{main::g}{SCALAR} might be undefined, but it is a bona fide SCALAR ref. Background: I'd like to import a variable into main:: , but carp or croak if that variable is already declared. EDIT Added an f subroutine in response to @DVK's initial answer. ANSWER (2010-07-27) This isn't easy, but it is possible. An eval technique is most portable, working on