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
11
votes
2 answers

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. The darget data type bool is an exception: In…
towi
  • 21,587
  • 28
  • 106
  • 187
11
votes
3 answers

Compiler replaces explicit cast to my own type with explicit cast to .NET type?

I have the following code: public struct Num { private readonly T _Value; public Num(T value) { _Value = value; } static public explicit operator Num(T value) { return new Num(value); …
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
11
votes
2 answers

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 this problem? I'm using Visual Studio 2012 RC.
Nick
  • 10,309
  • 21
  • 97
  • 201
10
votes
1 answer

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 combination of std::is_constructible and…
Constructor
  • 7,273
  • 2
  • 24
  • 66
10
votes
1 answer

Why is this operator= call ambiguous?

I was making a thin derived class with a forwarding constructor. (Bear with me, I must use GCC 4.7.2, which lacks inherited constructors). On the first try, I forgot to add the explicit keyword and got an error. Could someone explain exactly why…
AndyJost
  • 1,085
  • 1
  • 10
  • 18
10
votes
2 answers

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 = new Intent(getApplicationContext(),…
user504342
  • 945
  • 2
  • 16
  • 36
10
votes
3 answers

Why are vector's multi-argument constructors taking construction parameters not marked "explicit"?

I observed the following vector constructors in the Standard C++ library explicit vector(size_type n); vector(size_type n, const T& value, const Allocator& = Allocator()); Is there a reason why the second constructor is not marked explicit? This…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
9
votes
5 answers

Strings and ints, implicit and explicit

Had a coworker ask me this, and in my brain befuddled state I didn't have an answer: Why is it that you can do: string ham = "ham " + 4; But not: string ham = 4; If there's an implicit cast/operation for string conversion when you are…
Rostov
  • 2,516
  • 2
  • 23
  • 17
9
votes
1 answer

Prevent implicit conversion but allow list initialisation?

Let's say I have a class FunctionWrapper defined like this: struct FunctionWrapper { FunctionWrapper(std::function f); // ... plus other members irrelevant to the question }; I'd like to prevent implicit conversions from…
9
votes
1 answer

Should I really massively introduce the explicit keyword?

When I used the (recently released) Cppcheck 1.69 on my code1, it showed a whole lot of messages where I expected none. Disabling noExplicitConstructor proved that all of them were of exactly this kind. But I found that I'm not the only one with a…
Wolf
  • 9,679
  • 7
  • 62
  • 108
9
votes
1 answer

Why doesn't Option Explicit catch ReDim ?

Does anyone know why this doesn't throw a Variable not defined error when I compile it? 'Class1.cls' Option Explicit Public Sub foo() ReDim fubar(1 To 2, 1 To 1) End Sub Am I misunderstanding how Option Explicit is supposed to work? Or is…
RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
9
votes
0 answers

removing an explicit mandatory label using icacls

I was doing some experimentation with integrity levels and icacls on Windows 7. I set mandatory integrity levels, so that I get something that looks like this: C:\Debug>icacls test.exe test.exe Everyone:(I)(RX) …
chut7
  • 103
  • 1
  • 4
8
votes
2 answers

.NET - How is explicit cast with "as" different (internally) from (someType)someobject, and why?

I understand that when you use an explicit cast like this: (someType)someobject you can get an invalid cast exception if someobject is not really someType. As well I understand that when you cast with as like this: myObject = someObject as…
richard
  • 12,263
  • 23
  • 95
  • 151
8
votes
1 answer

Django inside

Some front-end experts claim that wrapping an with the
kmt
  • 897
  • 2
  • 10
  • 19
8
votes
2 answers

explicit non-single parameter constructor

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) { /* ...…
axe
  • 2,331
  • 4
  • 31
  • 53