This question has this code snippet:
A::A(const char *pc) {
A(string(pc));
}
A::A(string s) {
vector<string> tmpVector;
tmpVector.push_back(s);
A(tmpVector); // <-- error
}
// Constructor
A::A(vector<string> filePathVector) {
}
The problem is that A(tmpVector);
conflicts with vector<string> tmpVector;
:
error: conflicting declaration 'A tmpVector'
error: 'tmpVector' has a previous declaration as 'std::vector<std::basic_string<char> > tmpVector'
The answer says:
This
A(tmpVector);
is the same as this
A tmpVector; // but there is already an object called tmpVector
With an added comment:
In this context, the () are superfluous.
My question is: why are the parenthesis superfluous? What exactly in the C++11 spec makes that so? I have not seen this before.
Difference between std::vector and std::array in C++Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements. As array is fixed size, once initialized can't be resized. Vector occupies more memory.
A vector is a one-dimensional data structure and all of its elements are of the same data type. A factor is one-dimensional and every element must be one of a fixed set of values, called the levels of the factor. A matrix is a two-dimensional data structure and all of its elements are of the same type.
Firstly, finding an item in a very small vector can easily be faster than the same thing in a map, because all the memory in a vector is always contiguous (and so plays more nicely with computers' caches and such things), and the number of comparisons needed to find something in a vector might be about the same as for ...
1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.
From §8 [dcl.decl] of the standard:
Declarators have the syntax:
declarator:
ptr-declarator
noptr-declarator parameters-and-qualifiers trailing-return-type
ptr-declarator:
noptr-declarator
ptr-operator ptr-declarator
noptr-declarator:
declarator-id attribute-specifier-seq_opt
noptr-declarator parameters-and-qualifiers
noptr-declarator [ constant-expression_opt] attribute-specifier-seq_opt
( ptr-declarator )
(Remainder of grammar omitted).
In particular, note that
ptr-declarator
is a declarator
.( ptr-declarator )
is a noptr-declarator
which in turn is a ptr-declarator
.In other words, you can have as many pairs of parentheses as you want and it's still a declarator. Now this causes an ambiguity in cases like T(x);
, which is resolved by §6.8 [stmt.ambig] of the standard:
There is an ambiguity in the grammar involving expression-statements and declarations: An expression statement with a function-style explicit type conversion (5.2.3) as its leftmost subexpression can be indistinguishable from a declaration where the first declarator starts with a (. In those cases the statement is a declaration.
The example accompanying that paragraph directly covers this case:
class T {
// ...
public:
T();
T(int);
T(int, int);
};
T(a); // declaration
T(*b)(); // declaration
T(c)=7; // declaration
T(d),e,f=3; // declaration
extern int h;
T(g)(h,2); // declaration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With