Questions tagged [explicit-conversion]

This tag is about the `Explicit` C++ keyword.

In C++, the compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use single parameter constructors to convert from one type to another in order to get the right type for a parameter. Adding explicit to a one parameter constructor prevents the compiler for doing so.

106 questions
1
vote
1 answer

static_cast() vs type ()

In the "Programming: Principles and Practice Using C++", "Section 8.5.7 Argument checking and conversion" the following example is given as evidence on how to properly convert types, but never clearly explains why you would use int() vs…
Ben Don
  • 43
  • 6
1
vote
0 answers

How to convert an explicit operator to a method group?

My class ClassName has an operator defined: public static explicit operator ClassName(Guid value) { return new ClassName(value); } This allows to "cast" this way: var guids = new[] { Guid.NewGuid, Guid.NewGuid() }; var classes = guids.Select(g…
nvoigt
  • 75,013
  • 26
  • 93
  • 142
1
vote
1 answer

Converting iterators and const_iterators

General context: I am trying to build a container that will behave as as wrapper around a multi-dimensional array of run time defined dimensions - in fact the underlying array is of course a 1D array of the total size. The main part is that operator…
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1
vote
1 answer

C++ explicit constructor that takes a pointer

I've recently stumbled across an explicit constructor that receives a single pointer argument. I wonder if the explicit keyword is necessary in this case? as there is no constructor for a pointer so there cannot be any implicit conversion. class Foo…
AMT
  • 1,016
  • 1
  • 7
  • 13
1
vote
1 answer

Explicit and implicit conversion

I am pretty surprised that this struct, which is only explicitly convertible to bool, works fine inside a if statement: struct A { explicit operator bool( ) const { return m_i % 2 == 0; } int m_i; }; int main() { A a{…
1
vote
1 answer

Char to Int explicit Conversion in C#

I was trying to learn explicit conversion in c#. First I explicitly converted string to int namespace MyfirstCSharpCode { class Program { static void Main(string[] args) { string str = "1234"; int i…
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
1
vote
2 answers

cast missed c# explicit conversion

I've got trouble with type conversion with following code: public class pr { private T tt; public pr( T value ) { this.tt = value; } public static explicit operator T(pr op) { return default(T); } …
1
vote
4 answers
1
vote
2 answers

Can keyword "explicit" be removed from a constructor in one specific template instantiation?

I'm trying to create a template class to enforce dimensional correctness (length divided by time gives speed, and so on). The short story: "Dimensionless" is one of the possible instantiations. It would be convenient if I could allow all…
user1476176
  • 1,045
  • 1
  • 7
  • 15
1
vote
2 answers

Is C# 5.0 explicit numeric conversion description correct?

Explicit numeric conversion from float and double to any integral type described in the C# 5.0 specification (paragraph 6.2.1) as follows: • For a conversion from float or double to an integral type, the processing depends on the overflow…
Tenere
  • 361
  • 5
  • 11
1
vote
3 answers

Explicit type casting in C++

I am trying to convert a C code to C++. In my .c file I've definitions like this: void services(void); void transfers(void); Further more, a thread will initialize the above two like this: _beginthread((void*) services,0,NULL); _beginthread((void*)…
user2754070
  • 509
  • 1
  • 7
  • 16
1
vote
2 answers

implicit conversion operator

I want to print all the arguments of function using variadic templates feature of C++11. And I did the following: struct concatenate { template< typename ...ARGS > explicit concatenate(ARGS const & ...args) { cat(args...); …
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
0
votes
2 answers

What is the difference between implicit and explicit casting in Java?

What is the easiest way to include both implicit and explicit casting to your code? It is a requirement for my Java project. Graphic g = getGraphics(); Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(5)); This is the only code I have.…
Print Yo
  • 3
  • 2
0
votes
0 answers

Why explicit conversion from object to long (Unboxing) is not allowed in C#?

I was reading unboxing and came across this code: object obj = 22; long l = (long)obj; when I ran this code, it throws an exception InvalidCastException I do not understand why? For example Microsoft documentation on Boxing and Unboxing shows…
0
votes
2 answers

Why can't we directly assign the address of a 2D array to a pointer?

Why can't we directly assign the address of a 2D array to a pointer? Is there any way we can assign it in a single line, not with the help of a for loop? Is there any other better approach? // Array of 5 pointers to an array of 4 ints int…