Skip to main content
Filter by
Sorted by
Tagged with
-1 votes
1 answer
100 views

struct ExplicitDefaultConstructor { explicit ExplicitDefaultConstructor() {} }; struct Material { ExplicitDefaultConstructor a; int b; }; int main() { Material{.b = 2 }; } It's ...
Zebrafish's user avatar
  • 16.3k
3 votes
0 answers
72 views

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 ...
Oersted's user avatar
  • 3,834
0 votes
2 answers
205 views

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 ...
ValeriyKarasikov's user avatar
0 votes
1 answer
47 views

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(...
CPW's user avatar
  • 189
1 vote
1 answer
162 views

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 ...
Caduchon's user avatar
  • 5,278
0 votes
0 answers
1k views

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 ...
glades's user avatar
  • 5,374
0 votes
1 answer
121 views

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::...
CosmicHusky's user avatar
9 votes
1 answer
220 views

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}}: #...
Fedor's user avatar
  • 24.7k
-1 votes
2 answers
1k views

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(...
ZR_xdhp's user avatar
  • 371
6 votes
1 answer
671 views

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... ...
Richard Fabian's user avatar
3 votes
1 answer
227 views

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 : ...
Itachi Uchiwa's user avatar
1 vote
1 answer
260 views

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 ...
thamas's user avatar
  • 315
3 votes
2 answers
2k views

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 ...
DDS's user avatar
  • 2,480
0 votes
1 answer
2k views

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 ...
einpoklum's user avatar
  • 137k
0 votes
0 answers
106 views

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 ...
user avatar
7 votes
2 answers
398 views

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 ...
Martin Fehrs's user avatar
  • 1,175
4 votes
1 answer
144 views

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 ...
Reizo's user avatar
  • 1,467
2 votes
1 answer
76 views

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 ...
user avatar
3 votes
1 answer
298 views

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 ...
Eduardo J. Sanchez's user avatar
10 votes
2 answers
425 views

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&...
AOK's user avatar
  • 573
5 votes
0 answers
554 views

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 ...
Julian's user avatar
  • 1,612
1 vote
2 answers
2k views

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; ...
bgp2000's user avatar
  • 1,144
6 votes
1 answer
135 views

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 ...
eivour's user avatar
  • 1,787
19 votes
1 answer
2k views

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 ...
Tristan Brindle's user avatar
1 vote
1 answer
552 views

I wanted to create a class MPSList where constructor has an explicit keyword associated with it. Following is the bare minimal code: class MPSList { ...
letsBeePolite's user avatar
10 votes
1 answer
334 views

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(...
Evg's user avatar
  • 26.6k
9 votes
2 answers
411 views

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 ...
dhaumann's user avatar
  • 1,698
127 votes
4 answers
21k views

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? };...
Peter G.'s user avatar
  • 15.2k
19 votes
5 answers
5k views

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? ...
johnbakers's user avatar
  • 24.9k
0 votes
1 answer
1k views

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::...
David Doria's user avatar
  • 10.4k
0 votes
0 answers
377 views

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 ...
Lingxi's user avatar
  • 15.1k
12 votes
2 answers
1k views

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 ...
Walter's user avatar
  • 45.8k
1 vote
3 answers
118 views

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(); ...
Narek's user avatar
  • 40.2k
3 votes
1 answer
300 views

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(); ...
anderas's user avatar
  • 5,874
2 votes
2 answers
1k views

I have this piece of code: class Enum { public: const int &value() const { return value_; } bool operator==(const Enum &other) const { return (...
Cross R's user avatar
  • 23
0 votes
2 answers
107 views

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 ...
divine-code's user avatar
19 votes
3 answers
2k views

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 }; }; ...
John Zwinck's user avatar
11 votes
1 answer
4k views

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 ...
TemplateRex's user avatar
  • 70.9k
2 votes
2 answers
132 views

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 ...
davidA's user avatar
  • 13.9k
6 votes
1 answer
512 views

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 ...
Patrick Fromberg's user avatar
8 votes
2 answers
2k views

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 ...
axe's user avatar
  • 2,381
6 votes
4 answers
1k views

I have a situation where no constructor appears to be called: #include <iostream> using namespace std; int main () { class yoyo { public: int i; yoyo() ...
user2518270's user avatar
8 votes
4 answers
155 views

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 ...
Eric's user avatar
  • 98.1k
8 votes
3 answers
1k views

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&...
Armen Tsirunyan's user avatar
8 votes
2 answers
4k views

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 ...
cfa45ca55111016ee9269f0a52e771's user avatar
0 votes
1 answer
1k views

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_;} ...
user avatar
10 votes
1 answer
745 views

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 ...
Klaim's user avatar
  • 70.2k
50 votes
1 answer
31k views

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 ...
oopsi's user avatar
  • 2,059
36 votes
2 answers
64k views

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 ...
Jan Turoň's user avatar
  • 33.1k
4 votes
3 answers
559 views

The following code sum up my problem : template<class Parameter> class Base {}; template<class Parameter1, class Parameter2, class Parameter> class Derived1 : public Base<Parameter>...
Vincent's user avatar
  • 61.1k