explicit-constructor

explicit non-single parameter constructor

心已入冬 提交于 2020-01-03 09:04:02
问题 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 X { public: explicit X(int a, int b) { /* ... */} }; 回答1: In C++03, and in this particular case, it makes no sense for a two parameter constructor to be marked explicit . But it could make sense here: explicit X(int i, int j=42); So, marking a two parameter constructor with explicit does not have

explicit non-single parameter constructor

巧了我就是萌 提交于 2020-01-03 09:03:46
问题 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 X { public: explicit X(int a, int b) { /* ... */} }; 回答1: In C++03, and in this particular case, it makes no sense for a two parameter constructor to be marked explicit . But it could make sense here: explicit X(int i, int j=42); So, marking a two parameter constructor with explicit does not have

What's the difference between explicit and implicit assignment in C++

和自甴很熟 提交于 2019-12-29 05:56:32
问题 int value = 5; // this type of assignment is called an explicit assignment int value(5); // this type of assignment is called an implicit assignment What is the difference between those, if any, and in what cases do explicit and implicit assignment differ and how? http://weblogs.asp.net/kennykerr/archive/2004/08/31/Explicit-Constructors.aspx EDIT: I actually just found this article, which makes the whole thing a lot clearer... and it brings up another question, should you ( in general ) mark

Implicit conversion from int to vector?

狂风中的少年 提交于 2019-12-23 07:55:35
问题 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<int> v); int main() { f(5); } What I cannot understand and am asking you to explain is why the following code compiles std::vector<std::vector<int>> graph(5, 5); Not only does it compile, it actually resizes graph to 5 and sets each element to a vector of five zeros, i.e. does the same as would the code

Why is the std::bitset constructor with an unsigned long long argument not marked as explicit?

老子叫甜甜 提交于 2019-12-22 04:03:48
问题 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 best-practice guidelines, this single-argument constructor is not marked as explicit . What is the rationale behind this? 回答1: Explicit construction The main objection against an explicit constructor is that copy-initialization from unsigned integers no longer works constexpr auto N = 64; std::bitset<N> b

Why is the std::bitset constructor with an unsigned long long argument not marked as explicit?

假装没事ソ 提交于 2019-12-22 04:03:11
问题 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 best-practice guidelines, this single-argument constructor is not marked as explicit . What is the rationale behind this? 回答1: Explicit construction The main objection against an explicit constructor is that copy-initialization from unsigned integers no longer works constexpr auto N = 64; std::bitset<N> b

C++ — Why should we use explicit in this constructor?

北战南征 提交于 2019-12-20 02:49:06
问题 Please refer to Wikipedia:Strategy Pattern (C++) class Context { private: StrategyInterface * strategy_; public: explicit Context(StrategyInterface *strategy):strategy_(strategy) { } void set_strategy(StrategyInterface *strategy) { strategy_ = strategy; } void execute() const { strategy_->execute(); } }; Why it is a good practice to use explicit for the constructor of Context? Thank you 回答1: Well, explicit constructors are always safe, but can be inconvenient. explicit ensures a compilation

Purpose of Explicit Default Constructors

故事扮演 提交于 2019-12-17 17:48:12
问题 I recently noticed a class in C++0x that calls for an explicit default constructor. However, I'm failing to come up with a scenario in which a default constructor can be called implicitly. It seems like a rather pointless specifier. I thought maybe it would disallow Class c; in favor of Class c = Class(); but that does not appear to be the case. Some relevant quotes from the C++0x FCD, since it is easier for me to navigate [similar text exists in C++03, if not in the same places] 12.3.1.3

Why is the constructor in this C++ code ambiguous and how do I fix it?

喜你入骨 提交于 2019-12-10 12:53:11
问题 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> template<typename data_type, typename eval_type, typename Type1, typename Type2> class A { public: using a_type = std::tuple<Type1, Type2>; using b_type = std::tuple<std::size_t,std::size_t>; inline explicit constexpr A(const std::function<data_type(a_type)>& Initializer, const std::function<eval_type(data_type)

Initializing array through explicit constructor

佐手、 提交于 2019-12-08 11:40:48
问题 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; explicit Symbol(const char*); }; Now I want to write an example for documentation purposes that initializes an array (array/vector/list - I don't care about the exact type) and I need the example to be as clear and concise as possible. Ideally it would look like this: Symbol symbols[] = { "a", "b", "c"}; That