Linked Questions

21 votes
1 answer
1k views

Why does calling a functor with an undeclared variable work? [duplicate]

class foo { public: bool operator () (int & i) { return true; } }; int main() { foo(WhyDoesThisCompile); return 0; } When passing WhyDoesThisCompile (without spaces) ...
DanielCollier's user avatar
9 votes
4 answers
2k views

redefinition of a variable with a different type [duplicate]

sorry for the question title but I didn't know a correct title for my problem. I have the following code example: struct test { test(int a) { } }; int main() { test(1); return 0; } ...
roohan's user avatar
  • 741
12 votes
1 answer
978 views

What does mean "int(i)=1;"? [duplicate]

I am new to C++, I see following syntax in c++ to initialize variable. int(i)=1; Then, I have compiled in G++ compiler and compiler did not give any error or warning. So, What does mean int(i)=1; ...
user avatar
6 votes
1 answer
405 views

Why does using parentheses with a default constructor result in creation of the variable? [duplicate]

After watching Louis Brandy talk at CppCon 2017 I was shocked to discover that this code actually compiles: #include <string> int main() { std::string(foo); return 0; } And for ...
Amomum's user avatar
  • 6,333
0 votes
1 answer
5k views

No default constructor exists for class c++ [duplicate]

Hello, I'm trying to instantiate an anonymous object with a std::string variable 'name'. But intellisenen gives me error saying E0291 no default constructor exists for class "Player" GoldGame ...
Michael Song's user avatar
-1 votes
1 answer
437 views

inserting a double and a pair in map and printing the map [duplicate]

I want to build a container for fractions. The key will represent the value of the fraction in double; The numerator and the denominator are stored in a pair. std::map<double, pair<int, int&...
Simona's user avatar
  • 63
1 vote
0 answers
80 views

Unusual variable declaration in C++ [duplicate]

// s isn't declared before std::string(s); s = "Hellow world"; It compiles and works as if s was declared as usual at least on msvc and gcc (C++14). Why is it so? Did not find explanation in ...
Ravishing Grimness's user avatar
0 votes
0 answers
62 views

C++ object declaration and initialization, odd syntax? [duplicate]

#include <iostream> class SomeClass { public: SomeClass(): something(100) { std::cout<<"constructing"<<std::endl; } ~SomeClass() { std::cout<<"...
gb12345's user avatar
  • 29
1 vote
0 answers
44 views

Parentheses in left side of variable declaration [duplicate]

I recently came across the following variable declaration: QString(newText) = QString("Placeholder"); I don't understand how to parse this line: the use of parentheses on the left size of the ...
Thomas Francois's user avatar
1 vote
0 answers
41 views

c++ classes - error: conflicting declaration ‘Base k’ [duplicate]

Just wanted to know, why i'm getting conflicting declaration error while calling the constructor?? class Base { int a; public: Base() { a = 50; }; Base(int a): a(a) { } }; ...
Akash's user avatar
  • 71
0 votes
0 answers
40 views

Why is the constructor not called when assigning a functor to a std::function? [duplicate]

I don't understand what happens with the following code using functors and std::function. My explanations are interlaced with the code (the concatenated code is compilable). #include <chrono> #...
Patrick B.'s user avatar
  • 12.1k
117 votes
2 answers
5k views

Program being compiled differently in 3 major C++ compilers. Which one is right?

As an interesting follow-up (not of big practical importance though) to my previous question: Why does C++ allow us to surround the variable name in parentheses when declaring a variable? I found out ...
Predelnik's user avatar
  • 5,126
50 votes
2 answers
32k views

initialization: parenthesis vs. equals sign [duplicate]

What's the difference between T a(b); and T a = b; and T a = T(b); ?
fredoverflow's user avatar
39 votes
2 answers
7k views

What is the purpose of a declaration like int (x); or int (x) = 10;

If you look at the grammar for *declarator*s in §8/4 you'll notice that a noptr-declarator can be written as (ptr-declarator), that is, it can be written as (declarator-id), which validates ...
Mao's user avatar
  • 1,075
22 votes
2 answers
1k views

Most vexing parse with array access

While looking at some C++03 code, I found an instance of the most vexing parse that confused me: #include <sstream> #include <string> int main(int, char** argv) { std::stringstream ...
Vittorio Romeo's user avatar

15 30 50 per page