107 questions
0
votes
1
answer
113
views
Missing round_to() function in PPP_support.h
I'm currently working through Bjarne Stroustrup's Programming: Principles and Practice Using C++ (3rd Edition).
The book mentions a function named round_to() is provided in PPP_support.h
When, on the ...
7
votes
2
answers
159
views
GCC14 performes multiple implicit conversions instead of one matching explicit conversion
#include <cstdio>
#include <string>
class A {
std::string data;
public:
A() = default;
explicit A (const char* data) : data(data) {}
operator const char* () const;
...
1
vote
1
answer
191
views
Different Behavior for const int and int in C++ template
When I use C++ template, I found that sometimes it gives a non-straightforward result.
#include <cstdlib>
#include <iostream>
template<class f0>
void func(const int& a1, f0& ...
1
vote
1
answer
130
views
C# Object List Type Change based on condition
I am developing a complex application on .NET 6 using Microsoft Blazor. There are many classes and many functions manipulating List<Object> with different way, but also there are cases where the ...
-1
votes
2
answers
1k
views
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 ...
0
votes
0
answers
44
views
Why explicit conversion from object to long (Unboxing) is not allowed in C#? [duplicate]
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 ...
2
votes
1
answer
165
views
explicit template instantiation of explicit operator bool
I'm trying to understand why I get a linker error
( error LNK2001: unresolved external symbol "public: __cdecl Foo<int>::operator bool(void)const_ )
With the following code. If I move the ...
6
votes
1
answer
676
views
Why is my explicit constructor creating this ambiguity for my conversion operator?
I'm unable to figure out why my conversion operator is considering the explicit constructor.
#include <utility>
template <typename T = void>
struct First
{
template <typename... ...
1
vote
2
answers
115
views
How is 'if (x)' where 'x' is an instance of a class, not an implicit conversion? [duplicate]
According to cppreference.com an explicit conversion function cannot be used for implicit conversions. As an example they have this:
struct B
{
explicit B(int) { }
explicit B(int, int) { }
...
4
votes
2
answers
158
views
Are potentially dangerous implicit conversions with std::pair intentional? Is there a way to avoid them?
I just realized this code compiles fine, but has undefined behavior and (naturally) crashes at runtime:
#include <map>
#include <memory>
#include <utility>
int main(int argc, char *...
0
votes
2
answers
210
views
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 ...
1
vote
2
answers
288
views
Why can't we directly assign arr to pointer to array
I have an array of 5 integers.
arr and &arr is same address.
So why number 2 gives compilation error and and 3 works fine.
int arr[5] = {1,2,3,4,5};
1. int *p = arr;
2. int (*p1)[5] = arr //...
0
votes
2
answers
606
views
Why non const reference cannot be initialized to an object of different type?
I was reading this book and it is written that We can assign const reference of one type to an object of any other type, and the reason was given that, internally compiler assign the Rvalue to the ...
3
votes
1
answer
353
views
defining operator + for double and string data types in cpp
I am trying to define operator + for string and double using the following function
string operator + (const double& b,const string a){
return to_string(b)+a;
}
When I am doing the following ...
-1
votes
1
answer
2k
views
Implicit conversion in c++ for between unsigned types of bit length ie uint8_t,uint16_t
I would like to know how to implicit casting works in case of expressions between unsigned int of various bits ie uint8_t,uint16_t etc and ways to avoid it explicitly. For this reason i sumarrized ...
0
votes
1
answer
2k
views
Is there really no explicit constructor of std::string from an std::string_view?
Some (many?) programmers who are introduced to both std::string_view and std::string ask themselves: "Why can I convert the latter into the former, but not the other way around?"
One part ...
0
votes
1
answer
986
views
explicit conversion to std::function
I'm trying to define explicit conversion from some class to std::function like this:
#include <functional>
class ExpInt { private:
const int value;
public:
ExpInt(const int v):value(v){...
0
votes
0
answers
207
views
Implicit/Explicit conversion between two lists/IEnumerable in c# [duplicate]
Can I make Implicit/Explicit conversion between two lists i.e.
List<Person> person = new List<Human>();
1
vote
1
answer
532
views
implicit and explicit conversion from my class to string
My class is like:
class X {
public:
:
:
:
operator const char*() const { return "foo"; };
operator std::string() const { return std::string( "foo" ); };
:
...
2
votes
1
answer
549
views
C++: Compilation error with explicit keyword
The following code throws compilation error:
#include <stdio.h>
class Option
{
Option() { printf("Option()\n"); };
public:
explicit Option(const Option& other)
{
printf(...
-8
votes
2
answers
131
views
C# cast from IEnumerable<T> to T
Can anyone explain that :
compile OK
run-time KO
I already checked source code for IEnumerable<> & IEnumerable and found nothing
FYI this doesn't work with List
class Program
{
public ...
2
votes
0
answers
321
views
Why does byte to char conversion needs an int widening conversion
According to the specification
The following conversion combines both widening and narrowing primitive conversions:
byte to char
First, the byte is converted to an int via widening primitive ...
8
votes
3
answers
2k
views
if (static_cast<bool>(x)) vs if (x)
I have a coworker who routinely does an explicit cast to bool in conditionals, as in:
SomeType *ptr = /* some value */;
if (static_cast<bool>(ptr)) {
// do something
}
But I haven't been ...
3
votes
1
answer
136
views
Behavior when both conversion constructor and operator are present and explicitness is involved
I have a piece of code where I have both conversion constructor and conversion operator.
#include <iostream>
struct ClassFloat;
struct ClassInt{
int value;
ClassInt(int c) : value(c){...
2
votes
0
answers
63
views
Explanation for C# Language Specification: 6.2.4 Explicit reference conversions
As I mentioned in this post, I faced a for me not understandable compiler behaviour.
The code:
IEnumerable<IList<MyClass>> myData = //...getMyData
foreach (MyClass o in myData){}
It ...
1
vote
1
answer
305
views
static_cast<type>() vs type () [duplicate]
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 ...
0
votes
0
answers
202
views
How does explicit coercion works in R?
I am trying to understand the COERCION in R programming language. When it comes to explicit coercion, it states that we can convert from one class of vectors(considering general object) to another ...
-4
votes
2
answers
1k
views
Conversion of array string to array ulong in c#
I have a array of strings having values
string[] words = {"0B", "00", " 00", "00", "00", "07", "3F", "14", "1D"};
I need it to convert into array of ulong
ulong[] words1;
How should I ...
-1
votes
1
answer
114
views
How to TypeCast Dictionary of GenericType
I have a dictionary object like :-
var gridContent = new Dictionary<T, KeyValuePair<int, bool>>();
Now I want to typeCast this into :-
Dictionary<SomeClass, KeyValuePair<int, bool&...
1
vote
0
answers
93
views
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....
1
vote
1
answer
2k
views
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 ...
17
votes
6
answers
3k
views
Why do switch and if statements behave differently with conversion operators?
Why do switch and if statements behave differently with conversion operators?
struct WrapperA
{
explicit operator bool() { return false; }
};
struct WrapperB
{
explicit operator int() { ...
3
votes
2
answers
232
views
Why Doesn't This Do an Implicit Cast to the Converting Constructor?
So I have this code:
struct Foo {
Foo() { cout << "default\n"; }
Foo(const long long) { cout << "implicit\n"; }
};
struct Bar {
Bar(const short param) : param(param) {}
...
6
votes
1
answer
583
views
How to convert primitive type value to enum value, when enum contains elements with the same values?
I wrote the code :
enum FlipRotate2dEnum : byte {
NO = 0, None = 0,
R2 = 1, RotateTwice = 1,
FX = 2, FlipX = 2,
FY = 3, FlipY ...
0
votes
2
answers
721
views
How is short 128 = byte -128 while explicit casting in java ?
import java.util.Scanner;
public class ShortToByte{
public static void main (String args[]){
int i=0;
while (i<6){
Scanner sinput = new Scanner (System.in);
short a = sinput....
0
votes
1
answer
199
views
Explicit conversion with Assignment
A simple class with explicit conversion constructor.
class MyDouble {
double d;
public:
MyDouble() : d(0) {}
explicit MyDouble(double d_) : d(d_) {}
MyDouble & operator =(double ...
2
votes
1
answer
51
views
compiler failed to apply explicit conversion to an expression used as a condition
Here is my code:
#include <iostream>
using namespace std;
class Sales{
public:
Sales(int i = 0):i(i){}
explicit operator int(){ return i; }
private:
int i;
};
int main(){
...
2
votes
2
answers
993
views
Require operator double() to be explicitly called via static_cast<double>(x) [duplicate]
I'd like to enable conversion of my class to a double value. This can be achieved by overloading operator double() but this then allows for implicit conversion, which ideally I'd like to be able to ...
4
votes
1
answer
688
views
Priority and ambiguity of explicit conversion operator templates
I've been playing around with templated explicit conversion operators in my project, to implement explicit conversion from custom variant-like type. The minimal example reproducing my problem looks ...
2
votes
1
answer
727
views
C++ explicit constructor that takes a pointer [duplicate]
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 ...
1
vote
1
answer
195
views
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
votes
1
answer
119
views
operator= doesn't work properly in my custom string class
I'm trying to create a custom string class using std::vector< char >.
Please take a look at my code below.
#include <iostream>
#include <vector>
class mString {
public:
mString(...
0
votes
0
answers
28
views
Explicit Cast Syntax [duplicate]
What is the difference between the two lines of code below? Functionally, I believe them to be identical, but is there a certain case or best practice that would dictate which one I should use?
...
1
vote
1
answer
7k
views
Char to Int explicit Conversion in C# [duplicate]
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)
{
...
3
votes
1
answer
3k
views
Error defining explicit operator bool() outside class [duplicate]
I have problem defining operator bool() function outside the class
class A{
public:
explicit operator bool() const;
};
I am defining the function outside class as...
explicit A::operator bool()...
7
votes
2
answers
1k
views
Why C++ implicit conversion works, but explicit one does not?
The following code compiles successfully in C++11:
#include "json.hpp"
using json = nlohmann::json ;
using namespace std ;
int main(){
json js = "asd" ;
string s1 = js ; // <---- ...
13
votes
3
answers
3k
views
In C++, can we use { } for C-Style casting?
While I have been reading about datatype conversion, I saw this example:
void intval()
{
for (char c; cin >> c; )
cout << "the value of '" << c << "' is " << int{...
3
votes
1
answer
6k
views
How to convert a List to another List with explicit conversion operator without using extension method? [duplicate]
I added this conversion operator to my class and it works well.
When I pass an object of class A, it converts it to an object of class B.
public static explicit operator B(A a)
{
//Convert A to B
}...
0
votes
2
answers
151
views
Casting between different datatypes in C++
Working on casting between different datatypes in C++... The program here below prints:
>"Number is 2"
>"Number is 2.5"
>"Number is 2"
Please, explain why the last printout is not "Number ...
-2
votes
3
answers
59
views
To what should I explicitly convert this type?
With this code:
UnitReportPairGenerateValsModel.GenerateVals generateVals =
(from DataRow row in UnitReportPairGenerateValsDT.Rows
select new UnitReportPairGenerateValsModel.GenerateVals
...