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
explicit
404
questions
3659
votes
11
answers
1.2m
views
What does the explicit keyword mean?
What does the explicit keyword mean in C++?
122
votes
2
answers
8k
views
Why is #include <string> preventing a stack overflow error here?
This is my sample code:
#include <iostream>
#include <string>
using namespace std;
class MyClass
{
string figName;
public:
MyClass(const string& s)
{
figName = s;
...
88
votes
1
answer
28k
views
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 ...
83
votes
9
answers
56k
views
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 ...
62
votes
8
answers
25k
views
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 ...
49
votes
2
answers
30k
views
What is the purpose of "explicit" for a default constructor?
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 ...
47
votes
1
answer
28k
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 ...
45
votes
4
answers
17k
views
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 ...
42
votes
2
answers
11k
views
Does "explicit" keyword have any effect on a default constructor? [duplicate]
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()
...
39
votes
5
answers
18k
views
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 ...
38
votes
1
answer
12k
views
Why can't I use interface with explicit operator? [duplicate]
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 ...
35
votes
2
answers
8k
views
What is the purpose of the explicit keyword on a multi-argument constructor?
I recently came across some weird looking class that had three constructors:
class Class
{
public:
explicit Class(int );
Class(AnotherClass );
explicit Class(...
28
votes
5
answers
23k
views
Why can't a pointer be automatically converted into a unique_ptr when returning it?
Consider the following program:
#include <memory>
std::unique_ptr<int> get_it() {
auto p = new int;
return p;
}
int main() {
auto up ( get_it() );
return 0;
}
This fails ...
27
votes
1
answer
2k
views
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 ...
24
votes
3
answers
34k
views
Explicit copy constructor
I have extended std::string to fulfil my needs of having to write custom function build into string class called CustomString
I have defined constructors:
class CustomString : public std::string ...
24
votes
3
answers
3k
views
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-...
21
votes
5
answers
76k
views
Explicitly Define Datatype in Python Function
I want to define 2 variables in python function and define them as float explicitly. However, when i tried to define them in the function parameter, it's showing syntax error.
Please help me get the ...
21
votes
5
answers
43k
views
Android explicit intent with target component
Is it possible to fire explicit intent but not for an activity from my project but for activity in some other application.
I am sure of this code and I know it is running
Intent i=new Intent(this,...
20
votes
2
answers
1k
views
What changes to C++ made copy initialization work for class with explicit constructor?
Consider this code:
struct X{
explicit X(){}
explicit X(const X&){}
};
void foo(X a = X()){}
int main(){}
Using C++14 standard, both GCC 7.1 and clang 4.0 rejects the code, which is ...
19
votes
8
answers
20k
views
Best (safest) way to convert from double to int
I'm curious as to the best way to convert a double to an int. Runtime safety is my primary concern here (it doesn't necessarily have to be the fastest method, but that would be my secondary concern). ...
19
votes
2
answers
18k
views
Incorrectly aligned or overlapped by a non-object field error
I'm trying to create the following structure:
[StructLayout(LayoutKind.Explicit, Size=14)]
public struct Message
{
[FieldOffset(0)]
public ushort X;
[FieldOffset(2)...
19
votes
1
answer
5k
views
Why do we still need a .lib stub file when we've got the actual .dll implementation?
i'm wondering why linkers can not do their job simply by consulting the information in the actual .dll files that got the actual implementation code ? i mean why linkers still need .lib files to do ...
17
votes
5
answers
31k
views
Explicitly disabling UIView animation in iOS4+
I have been reading that Apple recommends to use block-based animations instead of CATransaction
Before, I was using this code to disable animations:
[CATransaction begin];
[CATransaction ...
17
votes
6
answers
7k
views
What variable name do you use for file descriptors?
A pretty silly trivial question. The canonical example is f = open('filename'), but
f is not very descriptive. After not looking at code in a while,
you can forget whether it means
"file" or "...
17
votes
5
answers
20k
views
Need explanation difference between json and dict in Python
I am just curious to understand JSON and Dict in Python more deeply.
I have a JSON response from a server like this:
`{"city":"Mississauga","country":"Canada","countryCode":"CA"}`
And I want to ...
17
votes
1
answer
376
views
Why double parameter constructor begins with an explicit keyword?
My buddy and I have been recently reading leveldb source code. And we encounter this problem. In leveldb db/skiplist.h file, there is a constructor declaration:
explicit SkipList(Comparator cmp, ...
17
votes
1
answer
386
views
Explicit cast operator fails with "assembly is not referenced" error
This is a very uncommon problem and there are definetly many workarounds, but I would like to understand what is actually going on and why it's not working.
So I have 3 assemblies in a test solution, ...
16
votes
5
answers
2k
views
Why Can A C# Class Inherit From One Interface Both Implicitly and Explicitly?
Today I happens to find that one C# class can inherit one interface both in implicit and explicit way. This surprises me. If C# works in this way, then one instance can behave differently when ...
16
votes
3
answers
35k
views
How to calculate the explicit form of a recursive function?
I have this recursive function:
f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
f(1) = 2
f(2) = 8
I know from experience that explicit form of it would be:
f(n) = 3 ^ n - 1 // pow(3, n) - 1
I wanna know if ...
16
votes
1
answer
12k
views
Why are function template specializations not allowed inside a class?
After having found answers to many of my questions on stackoverflow, I have now come up against a question of which I can't find the answer and I hope that someone is willing to help me!
My problem ...
15
votes
7
answers
2k
views
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
...
15
votes
3
answers
1k
views
Why const char* implicitly converted to bool rather than std::string?
#include <iostream>
#include <string>
struct mystruct{
mystruct(std::string s){
std::cout<<__FUNCTION__ <<" String "<<s;
}
...
15
votes
1
answer
7k
views
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 ...
14
votes
13
answers
1k
views
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 ...
14
votes
1
answer
12k
views
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):
...
13
votes
5
answers
30k
views
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 ...
13
votes
2
answers
876
views
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()...
13
votes
3
answers
22k
views
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 ...
13
votes
1
answer
3k
views
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' ...
13
votes
1
answer
2k
views
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 ...
12
votes
10
answers
2k
views
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 ...
12
votes
5
answers
3k
views
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 ...
12
votes
2
answers
1k
views
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 ...
12
votes
2
answers
396
views
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 ...
12
votes
1
answer
359
views
`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 -...
11
votes
3
answers
761
views
Compiler replaces explicit cast to my own type with explicit cast to .NET type?
I have the following code:
public struct Num<T>
{
private readonly T _Value;
public Num(T value)
{
_Value = value;
}
static public explicit operator Num<T>(T ...
11
votes
2
answers
5k
views
explicit operator bool error
I get the Compiler Error C2071 when I try to implement the explicit operator bool:
class C
{
public:
explicit operator bool() const
{
return !!*this;
}
};
Why? How can I solve ...
11
votes
2
answers
2k
views
Implicit conversion to explicit bool-types for sorting containers?
I am playing around with the new explicit for cast-operators. If you write something like
struct Data {
explicit operator string();
};
It is not possible to accidentally convert Data to string. ...
10
votes
1
answer
2k
views
How to check if type is explicitly/implicitly constructible?
How can it be checked that some type is explicitly (or vice versa implicitly) constructible from other type? Is it any SFINAE trick in this situation?
I can write is_explicitly_constructible as a ...
10
votes
2
answers
18k
views
starting android service using explicit vs implicit intent
According to the standard Android documentation, the prefered way to start a service (started service that is) is to use an explicit intent like this:
// Using explicit intent:
Intent serviceIntent = ...