Questions tagged [explicit]

In C++ specifies constructors and conversion operators that don't allow implicit conversions or copy-initialization. In C# declares a user-defined type conversion operator that must be invoked with a cast. For MS SQL Server for-xml-EXPLICIT mode use the [for-xml-explicit] tag

405 questions
14
votes
7 answers

Casting "number 0" to char before appending it

Why would I need to explicitly cast number 0 to char before appending it to string using string::operator+? using namespace std; int main() { string s = ""; s += 65; // no compile error s += (char)0; // requires explicit cast //s…
B Faley
  • 17,120
  • 43
  • 133
  • 223
14
votes
3 answers

Why const char* implicitly converted to bool rather than std::string?

#include #include struct mystruct{ mystruct(std::string s){ std::cout<<__FUNCTION__ <<" String "<
14
votes
1 answer

usage of explicit keyword for constructors

I was trying to understand the usage of explicit keyword in c++ and looked at this question on SO What does the explicit keyword mean in C++? However, examples listed there (actually both top two answers) are not very clear regarding the usage. For…
SandBag_1996
  • 1,570
  • 3
  • 20
  • 50
14
votes
13 answers

Why is "explicitness" considered a Good Thing?

I often hear people praise languages, frameworks, constructs, etc. for being "explicit". I'm trying to understand this logic. The purpose of a language, framework, etc. is to hide complexity. If it makes you specify all kinds of details…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
13
votes
3 answers

Explicit Assignment vs Implicit Assignment

I'm reading a tutorial for C++ but it didn't actually give me a difference (besides syntax) between the two. Here is a quote from the tutorial. You can also assign values to your variables upon declaration. When we assign values to a variable…
Freesnöw
  • 30,619
  • 30
  • 89
  • 138
13
votes
2 answers

explicit specifier doesn't seem to work when converting an object to bool

I am learning C++ recently and I noticed an example on cppreference, part of which goes like this: struct B { explicit B(int) { } explicit operator bool() const { return true; } }; int main() { B b2(2); // OK:…
Jinlong Chen
  • 141
  • 4
13
votes
1 answer

Initialization with empty curly braces

First attempt and everything works fine: class Base { public: Base() {std::cout << "default ctor!\n"; } }; ... Base b{}; Base b_one = {}; Another way of implementation(add explicit): class Base { public: explicit Base() {std::cout <<…
Gusev Slava
  • 2,136
  • 3
  • 21
  • 26
13
votes
5 answers

What is the difference between "Explicitly" and "Implicitly" in programming language?

I would like to have a clear and precise understanding of the difference between the two. Also is the this keyword used to implicitly reference or explicitly ? This is also why I want clarification between the two? I assume to use the this keyword…
Equivocal
  • 912
  • 2
  • 14
  • 27
13
votes
1 answer

Explicit copy constructor and std::sort

When sorting a container of objects having an explicit copy ctor I get compiler errors (from g++ 4.8.2 and clang++ 3.4, both in -std=c++11 mode) that I don't understand. I've created a simple example to demonstrate the problem class A { public: …
13
votes
1 answer

Why is `explicit` not compatible with `virtual`?

struct A { // error C2216: 'explicit' cannot be used with 'virtual' virtual explicit operator bool() const { return true; } }; struct B : A { // error C2216: 'explicit' cannot be used with 'override' explicit…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
12
votes
2 answers

Why conversion function is not used for an object when it is assigned to?

I learned that we could provide conversion operators for our classes in C++. So I expected that for the following program, c=1; would've used the conversion operator int(). But to my surprise; that doesn't happen and we get a compiler error saying…
12
votes
1 answer

`auto x = type{...}` initialization syntax and `explicit` conversion operator - clang vs gcc

Given this code (on wandbox): struct X { explicit operator int() { return 0; } }; int main() { auto y = int{X{}}; } And the following compiler options: -std=c++1z -Wall -Wextra -Wpedantic g++ (tested versions: 7, 6.1, 5.3) refuses to…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
12
votes
2 answers

F#: explicit type parameters in operator binding

I'm trying to define operator with the explicit type parameters and constraints: let inline (===)<'a, 'b when 'a : not struct and 'b : not struct> a b = obj.ReferenceEquals (a,b) It works well in F# 2.0, but produces the: warning FS1189:…
controlflow
  • 6,667
  • 1
  • 31
  • 55
12
votes
5 answers

Why does the compiler choose bool over string for implicit typecast of L""?

Having recently introduced an overload of a method the application started to fail. Finally tracking it down, the new method is being called where I did not expect it to be. We had setValue( const std::wstring& name, const std::wstring& value…
Greg Domjan
  • 13,943
  • 6
  • 43
  • 59
12
votes
10 answers

Foo f = Foo(); // no matching function for call to 'Foo::Foo(Foo)' ... huh?

class Foo { public: explicit Foo() {} explicit Foo(Foo&) {} }; Foo d = Foo(); error: no matching function for call to 'Foo::Foo(Foo)' I tried changing Foo(Foo&) to Foo(Foo) as the error suggests, which AFAIK is not a valid constructor,…
Kyle
  • 4,487
  • 3
  • 29
  • 45
1 2
3
26 27