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
Questions tagged [explicit]
405 questions
3615
votes
11 answers
What does the explicit keyword mean?
What does the explicit keyword mean in C++?

Skizz
- 69,698
- 10
- 71
- 108
122
votes
2 answers
Why is #include preventing a stack overflow error here?
This is my sample code:
#include
#include
using namespace std;
class MyClass
{
string figName;
public:
MyClass(const string& s)
{
figName = s;
}
const string& getName() const
{
return…

airborne
- 3,664
- 4
- 15
- 27
88
votes
1 answer
Can a cast operator be explicit?
When it comes to constructors, adding the keyword explicit prevents an enthusiastic compiler from creating an object when it was not the programmer’s first intention. Is such mechanism available for casting operators too?
struct Foo
{
operator…

qdii
- 12,505
- 10
- 59
- 116
83
votes
9 answers
explicit and implicit c#
I'm new to C# and learning new words. I find it difficult to understand what's the meaning of these two words when it comes to programming c#.
I looked in the dictionary for the meaning and here's what I got:
Implicit
"Something that is implicit…

tintincutes
- 5,618
- 25
- 67
- 86
60
votes
8 answers
Can you use keyword explicit to prevent automatic conversion of method parameters?
I know you can use C++ keyword 'explicit' for constructors of classes to prevent an automatic conversion of type. Can you use this same command to prevent the conversion of parameters for a class method?
I have two class members, one which takes a…

Superpolock
- 3,515
- 7
- 30
- 24
49
votes
2 answers
Purpose of Explicit Default Constructors
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…

Dennis Zickefoose
- 10,791
- 3
- 29
- 38
45
votes
1 answer
C++ always use explicit constructor
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 out this article…

oopsi
- 1,919
- 3
- 21
- 28
42
votes
4 answers
Why can't I call methods within a class that explicitly implements an interface?
Here's the story. I created an interface, IVehicle. I explicitly implemented the interface in my class, Vehicle.cs.
Here is my interface:
Interface IVehicle
{
int getWheel();
}
here is my class:
class Vehicle: IVehicle
{
public int…

tyrone302
- 685
- 1
- 7
- 6
40
votes
2 answers
Does "explicit" keyword have any effect on a default constructor?
Is there a reason to use the explicit keyword for a constructor that doesn't take any arguments? Does it have any effect? I'm wondering because I just came across the line
explicit char_separator()
near the end of the page documenting…

Kilian Brendel
- 525
- 1
- 5
- 16
39
votes
5 answers
Why is explicit allowed for default constructors and constructors with 2 or more (non-default) parameters?
I understand that constructors with one (non-default) parameter act like implicit convertors, which convert from that parameter type to the class type. However, explicit can be used to qualify any constructor, those with no parameters (default…

Ashwin Nanjappa
- 76,204
- 83
- 211
- 292
36
votes
1 answer
Why can't I use interface with explicit operator?
I'm just wondering if anyone knows the reason why you are not allowed to use interfaces with the implicit or explicit operators?
E.g. this raises compile time error:
public static explicit operator MyPlayer(IPlayer player)
{
...
}
"user-defined…

theburningmonk
- 15,701
- 14
- 61
- 104
34
votes
1 answer
Explicit keyword on multi-arg constructor?
I recently came across some weird looking class that had three constructors:
class Class
{
public:
explicit Class(int );
Class(AnotherClass );
explicit Class(YetAnotherClass, AnotherClass );
// ...
}
This doesn't…

LiraNuna
- 64,916
- 15
- 117
- 140
27
votes
1 answer
Virtual explicit conversion operator overriding
I have a class Base defining an explicit operator bool:
struct Base {
virtual explicit operator bool() const {
return true;
}
};
And I have a subclass Derived, defining an operator bool:
struct Derived : Base {
operator bool()…

phimuemue
- 34,669
- 9
- 84
- 115
27
votes
5 answers
Why can't a pointer be automatically converted into a unique_ptr when returning it?
Consider the following program:
#include
std::unique_ptr get_it() {
auto p = new int;
return p;
}
int main() {
auto up ( get_it() );
return 0;
}
This fails to compile with the following error:
a.cpp:5:9: error: could…

einpoklum
- 118,144
- 57
- 340
- 684
24
votes
3 answers
what is the usecase for explicit (bool)
C++20 introduced explicit (bool) which conditionally selects at compile-time whether a constructor is made explicit or not.
Below is an example which I found here.
struct foo {
// Specify non-integral types (strings, floats, etc.) require…

NKAR
- 348
- 1
- 5