75 questions
-1
votes
1
answer
100
views
Why does explicit default constructor prevent this from being constructed?
struct ExplicitDefaultConstructor
{
explicit ExplicitDefaultConstructor() {}
};
struct Material
{
ExplicitDefaultConstructor a;
int b;
};
int main() {
Material{.b = 2 };
}
It's ...
3
votes
0
answers
72
views
conditional operator: inconsistency between implicit conversion sequence used for different compilers
This is a follow-up of this question: conditional operator expression with base and const derived class doesn't compile, why?.
The core is cond ? [cv] T1() : [cv] T2() where, for instance T2 ...
0
votes
2
answers
205
views
Why can't the constructor be called explicitly?
I was looking into creating a constructor to initialize an encapsulated std::array and came across a problem that the constructor of a copyable type (class A) could not be called explicitly. There are ...
0
votes
1
answer
47
views
How parameter type conversion in C++ operator works? [duplicate]
I am creating a new class VarDbl, which contains a few explicit constructors as well as a + operator:
class VarDbl {
...
explicit VarDbl(double value, double uncertainty);
explicit VarDbl(...
1
vote
1
answer
162
views
Is there any reason to mark a constructor of an abstract class as explicit
In the following example, my class is abstract due to the abstract method run. I also have a constructor from another type. I always mark constructor having only 1 argument as explicit, except when I ...
0
votes
0
answers
1k
views
error: use of deleted function operator=(&&) with std::optional
I got myself an error sprouting from std::optional. Now when trying to reconstruct it, there seems to be something going on with defaulted ctors that I don't yet understand. Consider the following ...
0
votes
1
answer
121
views
Why is there no implicit conversion sequence from int to vector<double>?
Regarding
vector<double> v2 = 9; //error: no conversion from int to vector
Is there no implicit conversion sequence of the copy-initialization from
int
to
vector<double>
because
std::...
9
votes
1
answer
220
views
Overload resolution between two constructors from std::initializer_list
In following program, struct C has two constructors : one from std::initializer_list<A> and the other from std::initializer_list<B>. Then an object of the struct is created with C{{1}}:
#...
-1
votes
2
answers
1k
views
C++ copy assignment operator behaviour
I used the code below to test the behaviour of copy assignment operator:
#include <iostream>
using namespace std;
int group_number = 10; // Global
class Player {
public:
explicit Player(...
6
votes
1
answer
671
views
Why is my explicit constructor creating this ambiguity for my conversion operator?
I'm unable to figure out why my conversion operator is considering the explicit constructor.
#include <utility>
template <typename T = void>
struct First
{
template <typename... ...
3
votes
1
answer
227
views
Why is the converting constructor preferred to the conversion operator?
I have this class SmallInt that should represent a positive integer value in the range 0-255-inclusive:
struct SmallInt{
explicit SmallInt(int x = 0) : iVal_( !(x < 0 || x > 255) ? x :
...
1
vote
1
answer
260
views
Can't add element into container<T> with emplace new if T has explicit constructor
I am writing a fixed size container type, with placement new.
When I was testing it I figured it out my "emplace_back()" like function does not compile if the type T has explicit ctor.
Here ...
3
votes
2
answers
2k
views
C++ use of explicit suggested by cppcheck
Is using the cast constructor bad?
Otherweise why a code quality checker (cppcheck in my case) would constantly suggest to add explicit before single parameter constructors?
What if I want to do
class ...
0
votes
1
answer
2k
views
Is there really no explicit constructor of std::string from an std::string_view?
Some (many?) programmers who are introduced to both std::string_view and std::string ask themselves: "Why can I convert the latter into the former, but not the other way around?"
One part ...
0
votes
0
answers
106
views
To use parameterized constructor through explicit call
I am facing an error while writing this code so here in the below program code the obj1 and obj2 are called explicitly by using parameterized constructor
But I am not able to get the output saying an ...
7
votes
2
answers
398
views
Why is the constructor of std::in_place_t defaulted and explicit?
cppreference shows the following definition of std::in_place_t:
struct in_place_t {
explicit in_place_t() = default;
};
inline constexpr std::in_place_t in_place{};
Why have they added an ...
4
votes
1
answer
144
views
C++: Particularities of considering but not calling constructors
On cppreference about list-initialization in the second intend (for copy-list-initialization) it says:
copy-list-initialization (both explicit and non-explicit constructors are considered, but only ...
2
votes
1
answer
76
views
Avoid spelling out type in artificially amgibuous overloaded function call
Minimal example program:
#include <vector>
void f(std::vector<int>) {} // #1
void f(std::vector<void *>) {} // #2
int main() { f({ 1 }); }
It would intuitively make sense for ...
3
votes
1
answer
298
views
Copy constructor and overloaded addition operator
I am reviewing operator overloading in C++. Just for fun I am implementing a BigInt class.
The first operator I want to overload for it is the addition operator. I have decided to overload this ...
10
votes
2
answers
425
views
Why is the constructor in this C++ code ambiguous and how do I fix it?
In the below code, the compiler can't figure out which constructor I want to use. Why, and how do I fix this? (Live example)
#include <tuple>
#include <functional>
#include <iostream&...
5
votes
0
answers
554
views
Explicit constructor still allows for implicit conversion
I am sketching a small generic type-wrapper-template in C++14, that is intended to enable, disable, or extend the underlying type's interface using mixins.
Here is the code for this wrapper (stripped ...
1
vote
2
answers
2k
views
Initializing array through explicit constructor
I'm writing a class that has an explicit constructor taking a const char* argument. For the intents and purposes of this question it looks like this:
struct Symbol
{
Symbol()=default;
...
6
votes
1
answer
135
views
c++ explicit constructor called in implicit situation
I compiled the code below using g++ 6.3.0, with -std=c++14 option.
#include <utility>
#include <iostream>
struct A{
int x;
A(const A&)=default;
A(int x):x(x){}
};
struct ...
19
votes
1
answer
2k
views
Explicit default constructors in C++17
In C++17, empty tag types in the standard library now have default constructors which are marked explicit, and are also = default. For example, std::piecewise_construct_t is now defined as
struct ...
1
vote
1
answer
552
views
Incorrect use of explicit keyword in c++
I wanted to create a class MPSList where constructor has an explicit keyword associated with it.
Following is the bare minimal code:
class MPSList { ...
10
votes
1
answer
334
views
Inheriting an explicit constructor (Intel C++)
Intel C++ compiler (Version 16.0.3.207 Build 20160415) seems to drop the explicit specifier when the constructor of the base class is inherited with using. Is this a bug?
struct B
{
explicit B(...
9
votes
2
answers
411
views
Is removing 'explicit' from a constructor binary compatible?
An external library we are using contains the following explicit constructor:
class Chart {
public:
explicit Chart(Chart::Type type, Object *parent);
// ...
};
The compiler complains with the ...
127
votes
4
answers
21k
views
Explicit constructor taking multiple arguments
Does making a constructor having multiple arguments explicit have any (useful) effect?
Example:
class A {
public:
explicit A( int b, int c ); // does explicit have any (useful) effect?
};...
19
votes
5
answers
5k
views
Prevent undesired conversion in constructor
According to here, explicit:
Specifies constructors and conversion operators (since C++11) that
don't allow implicit conversions or copy-initialization.
Thus, are these two techniques identical?
...
0
votes
1
answer
1k
views
In-class member initializer of unique_ptr to nullptr error with explicitly defined default constructor
I have a class template that assigns a unique_ptr to nullptr using an in-class member initializer. If I use MyClass(){}, all is well. If I use MyClass() = default, I get:
conversion from 'std::...
0
votes
0
answers
377
views
Explicit std::unique_lock constructor from mutex
I see that the constructor overload that takes a mutex is marked explicit. I don't see the reason to specify it so. I think there is no harm to allow implicit conversion from mutex to a corresponding ...
12
votes
2
answers
1k
views
What's the reasoning behind std::unique_ptr<T>'s constructor from T* being explicit?
As std::unique_ptr provides a handy way to avoid memory leaks and ensure exception safety, it is sensible to pass them around rather than raw pointers. Thus, one may want (member) functions with a ...
1
vote
3
answers
118
views
Do we need explicit for CTOR with pointer type?
Do we need explicit in this case:
class A
{
explicit A(B* b);
};
I think that even if we do not mark the constructor as explicit, it will be a compilation error to write:
A a = new B();
...
3
votes
1
answer
300
views
Why can I assign a QObject* to a QObject?
Consider the following code:
#include <QObject>
class A : public QObject
{
Q_OBJECT
public:
A(QObject* parent = 0) : QObject(parent) {}
}
int main()
{
A a = new A();
...
2
votes
2
answers
1k
views
C++ 'no matching function for call to' and 'no known conversion for argument'
I have this piece of code:
class Enum {
public:
const int &value() const {
return value_;
}
bool operator==(const Enum &other) const {
return (...
0
votes
2
answers
107
views
Not getting the expected behavior with "explicit" keyword in c++
I have the following classes
class abc
{
private:
string name_;
public:
explicit abc(string name);
};
class xyz
{
private:
abc obj_abc_;
public:
xyz ():obj_abc_("NOTHING") { }; //I think ...
19
votes
3
answers
2k
views
C++11: in-class initializaton with "= {}" doesn't work with explicit constructor
In C++11 we can do in-class initialization using a "brace-or-equal-initializer" (words from the standard) like this:
struct Foo
{
/*explicit*/ Foo(int) {}
};
struct Bar
{
Foo foo = { 42 };
};
...
11
votes
1
answer
4k
views
Why is the std::bitset constructor with an unsigned long long argument not marked as explicit?
The Standard Library class template std::bitset<N> has a constructor (C++11 and onwards, unsigned long argument before C++11)
constexpr bitset(unsigned long long) noexcept
Contrary to many ...
2
votes
2
answers
132
views
C++ - why does this code compile when there's no obvious constructor match?
Please consider the following code:
class Foo {
public:
explicit Foo(double) {}
};
Foo * test();
Foo * test() {
return new Foo(Foo(1.0)); // (1)
}
My question concerns line (1). This is very ...
6
votes
1
answer
512
views
This is not copy-initializing, or is it?
In the following code I am not allowed to declare an explicit ctor because the compiler says I am using it in a copy-initializing context (clang 3.3 and gcc 4.8).
I try to prove the compilers wrong by ...
8
votes
2
answers
2k
views
explicit non-single parameter constructor [duplicate]
Can anyone explain why does non-single parameter constructor marked as explicit compile?
As far as I understand this is absolutely useless keyword here, so why does this compile without error?
class ...
6
votes
4
answers
1k
views
When is a c++ constructor not called?
I have a situation where no constructor appears to be called:
#include <iostream>
using namespace std;
int main ()
{
class yoyo
{
public:
int i;
yoyo()
...
8
votes
4
answers
155
views
Can I overload an implicit initialization to 0?
Is it possible to write a class such that these are valid:
Foo a;
Foo b = 0;
Foo c = b;
Foo d(0);
Foo e(1);
Foo f = Foo(1);
But these are not:
int x;
Foo a = x;
Foo b = 1;
Foo c = 2;
//etc
...
8
votes
3
answers
1k
views
Why can a std::vector be constructed using two ints?
vector<T> has a constructor that takes the size of the vector, and as far as I know it is explicit, which can be proved by the fact that the following code fails to compile
void f(std::vector&...
8
votes
2
answers
4k
views
C++11 initializer_list constructor marked "explicit"
Can I use explicit with an init-list ctor to make sure an expression like {a} doesn't result in unexpected implicit conversion? And another thought: should I be worried about it? Writing {a} is less ...
0
votes
1
answer
1k
views
Explicit constructor and overloading
template<typename T>
class RAII
{
public:
explicit RAII( T* p = 0 ): p_(p){}
~RAII() {delete p_;}
T& operator*() const { return p_;}
T* operator‐>() const{ return p_;}
...
10
votes
1
answer
745
views
Explicit on N-ary constructors?
In this presentation: http://qtconference.kdab.com/sites/default/files/slides/mutz-dd-speed-up-your-qt-5-programs-using-c++11.pdf
The author suggests that N-ary constructors benefit from the C++11 ...
50
votes
1
answer
31k
views
When to make constructor explicit in C++ [closed]
After reading the following blog :
http://xania.org/200711/ambiguous-overloading
I started asking myself "should I not always explicit define my constructors?"
So I started reading more than found ...
36
votes
2
answers
64k
views
What is the difference between implicit constructors and default constructors?
This is very trivial, but Czech language (my native) doesn't distinguish between implicit and default, so I am confused by some Czech translations what is the difference between implicit and default ...
4
votes
3
answers
559
views
Implicit constructor available for all types derived from Base excepted the current type?
The following code sum up my problem :
template<class Parameter>
class Base {};
template<class Parameter1, class Parameter2, class Parameter>
class Derived1 : public Base<Parameter>...