Skip to main content
Filter by
Sorted by
Tagged with
0 votes
1 answer
113 views

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 ...
Khavin S's user avatar
7 votes
2 answers
159 views

#include <cstdio> #include <string> class A { std::string data; public: A() = default; explicit A (const char* data) : data(data) {} operator const char* () const; ...
Martin Horský's user avatar
1 vote
1 answer
191 views

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& ...
alimadodo's user avatar
1 vote
1 answer
130 views

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 ...
Stavros Koureas's user avatar
-1 votes
2 answers
1k views

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 ...
Print Yo's user avatar
0 votes
0 answers
44 views

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 ...
Creative Learner's user avatar
2 votes
1 answer
165 views

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 ...
Tyson Hilmer's user avatar
6 votes
1 answer
676 views

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... ...
Richard Fabian's user avatar
1 vote
2 answers
115 views

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) { } ...
mayaknife's user avatar
  • 302
4 votes
2 answers
158 views

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 *...
user541686's user avatar
  • 213k
0 votes
2 answers
210 views

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 ...
Sanjeev's user avatar
  • 125
1 vote
2 answers
288 views

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 //...
Sanjeev's user avatar
  • 125
0 votes
2 answers
606 views

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 ...
Nikhil's user avatar
  • 308
3 votes
1 answer
353 views

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 ...
doer10195's user avatar
-1 votes
1 answer
2k views

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 ...
Theo Niko's user avatar
0 votes
1 answer
2k views

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 ...
einpoklum's user avatar
  • 138k
0 votes
1 answer
986 views

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){...
OrenIshShalom's user avatar
0 votes
0 answers
207 views

Can I make Implicit/Explicit conversion between two lists i.e. List<Person> person = new List<Human>();
Oluwadamilola Adegunwa's user avatar
1 vote
1 answer
532 views

My class is like: class X { public: : : : operator const char*() const { return "foo"; }; operator std::string() const { return std::string( "foo" ); }; : ...
Swiss Frank's user avatar
  • 2,520
2 votes
1 answer
549 views

The following code throws compilation error: #include <stdio.h> class Option { Option() { printf("Option()\n"); }; public: explicit Option(const Option& other) { printf(...
Saksham Jain's user avatar
-8 votes
2 answers
131 views

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 ...
sofiene reghima's user avatar
2 votes
0 answers
321 views

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 ...
user12208242's user avatar
8 votes
3 answers
2k views

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 ...
squatch's user avatar
  • 81
3 votes
1 answer
136 views

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){...
jannarc's user avatar
  • 118
2 votes
0 answers
63 views

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 ...
Emaborsa's user avatar
  • 2,950
1 vote
1 answer
305 views

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 ...
Ben Don's user avatar
  • 43
0 votes
0 answers
202 views

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 ...
Anil's user avatar
  • 1,778
-4 votes
2 answers
1k views

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 ...
Parnal p's user avatar
-1 votes
1 answer
114 views

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&...
Bhupesh's user avatar
  • 45
1 vote
0 answers
93 views

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....
nvoigt's user avatar
  • 77.8k
1 vote
1 answer
2k views

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 ...
Serge Ballesta's user avatar
17 votes
6 answers
3k views

Why do switch and if statements behave differently with conversion operators? struct WrapperA { explicit operator bool() { return false; } }; struct WrapperB { explicit operator int() { ...
nyarlathotep108's user avatar
3 votes
2 answers
232 views

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) {} ...
Jonathan Mee's user avatar
  • 39.1k
6 votes
1 answer
583 views

I wrote the code : enum FlipRotate2dEnum : byte { NO = 0, None = 0, R2 = 1, RotateTwice = 1, FX = 2, FlipX = 2, FY = 3, FlipY ...
leofun01's user avatar
  • 341
0 votes
2 answers
721 views

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....
noogler's user avatar
  • 187
0 votes
1 answer
199 views

A simple class with explicit conversion constructor. class MyDouble { double d; public: MyDouble() : d(0) {} explicit MyDouble(double d_) : d(d_) {} MyDouble & operator =(double ...
phy nju's user avatar
  • 289
2 votes
1 answer
51 views

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(){ ...
Yuan Wen's user avatar
  • 1,731
2 votes
2 answers
993 views

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 ...
Arc's user avatar
  • 149
4 votes
1 answer
688 views

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 ...
aclex's user avatar
  • 65
2 votes
1 answer
727 views

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 ...
AMT's user avatar
  • 1,026
1 vote
1 answer
195 views

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 == ...
nyarlathotep108's user avatar
0 votes
1 answer
119 views

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(...
Zack Lee's user avatar
  • 3,164
0 votes
0 answers
28 views

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? ...
Foxtrek_64's user avatar
1 vote
1 answer
7k views

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) { ...
Pரதீப்'s user avatar
3 votes
1 answer
3k views

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()...
user3922375's user avatar
7 votes
2 answers
1k views

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 ; // <---- ...
GetFree's user avatar
  • 43k
13 votes
3 answers
3k views

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{...
Shadi's user avatar
  • 1,791
3 votes
1 answer
6k views

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 }...
Amirhossein Yari's user avatar
0 votes
2 answers
151 views

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 ...
LastBlow's user avatar
  • 707
-2 votes
3 answers
59 views

With this code: UnitReportPairGenerateValsModel.GenerateVals generateVals = (from DataRow row in UnitReportPairGenerateValsDT.Rows select new UnitReportPairGenerateValsModel.GenerateVals ...
B. Clay Shannon-B. Crow Raven's user avatar