Strange class cast happening [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I received a code to review and I can't understand why a class cast is happening. Given the following dummy code:
class A {
int a;
void dummy() {
std::cout << "wewe";
};
};
class B {
int a;
void dummy() {
std::cout << "wewe";
};
};
A strangeCast() {
return B();
}
It builds with no errors on this project on VSCode on Linux.
I checked for any relationship between the two classes: no ereditariety, no overloading of the equal operator, no cast defined.
While if I try to do it in a Visual Studio Community 2019 on Windows, it tells me that class conversion is not defined (like I expect to).
Do you think I overlooked any of the above relationship or any other way for this to happen exists?
Edit: I know that is not a good code, that it has errors, but since it sounded so strange to me that it compiled without errors/warnings, I was just wondering if there was something I didn't know about.

Strange class cast happening
There is no cast whatsoever. Cast is an explicit conversion. What you're showing is an implicit conversion.
or any other way for this to happen exists?
The program is ill-formed. Only possibility for this to compile is a language extension of the compiler that you are using. Regardless, if there is no diagnostic message, then the compiler does not conform to the standard.

Related

Base class and templates [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Can I write code like this (tried it and doesn't compile in VS2015):
template<class BaseClassT>
class DerivedClass : public BaseClassT
{
...
};
and then use it like:
class BaseClass
{
};
DeriveClass<BaseClass> c;
if not possible, is there a way to implement the same idea?
Yes, you may use a template argument as base, and it compiles in MSVS if you fix the typo.
(Note that this is not the CRTP, despite what you may have heard.)

error: expected identifier before numeric constant Issue Once Again [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Team,
This question has been asked many times, but the answers don't seem to fit my situation. Here's my VERY simple code. The line of code that is causing the problem works perfectly in the main routine; however, when I try to embed an object within an object using the exact same code the error message shows up. I suspect the cause is silly, but I'm too close and can't see it...enter image description here
Within a class you have to use the member-initialization list to initialize your member variable A in B:
class B{
public:
A one; //Declare one here
int numbluemarbles;
B(): one(100){} //initialize one here
};
In c++11 or newer you can also use the newer syntax:
class B{
public:
A one = 100.0; //C++11
int numbluemarbles;
};
For more about how you can use c++11 initialization options have a read of this: http://www.informit.com/articles/article.aspx?p=1852519

boost::optional has changed an implicit function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using VS2008 and has just changed from boost 1.51 to 1.61. Unfortunately boost decided to change the boost::optional<T> and make the cast from optional to bool explicit i.e. the snippet below is not valid anymore
boost::optional<TYPE> optType;
TYPE t = default;
if (optType)
t = optType;
You must explicitly cast the optType to bool to make this work.
Even more unfortunate is the fact that this still compiles (with the wrong result ofc), and I am unable to detect where it is used. Does anyone know of any (even intrusive) methods to detect those places.
I tried to mark the implicit cast as deprecated in boosts 1.51 hpp-file but for some reason the compiler does not show any warnings.
The amount of code I have to look through is several hundred thousand rows that's why I need this.
In your particular snippet, you could simply write
boost::optional<TYPE> optType;
TYPE t = default;
if (optType)
t = true;
However, did you mean to get the TYPE value?
if (optType)
t = *optType;
You can also simply to
t = optType.get_value_or(default);

Accessing struct member through unique_ptr gives segmentation fault [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is how I set pointer to struct. At runtime segmentation fault is thrown at second line. LoadedPDFInfo is struct in Canvas namespace
struct std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo;
pdfInfo->handle = ++currentPDFHandle;
pdfInfo->totalPageNum = FPDF_GetPageCount(doc);
First, std::unique_ptr is a class not a struct, so get rid of the struct prefix on the pdfInfo variable declaration. You were probably thinking of this instead:
std::unique_ptr<struct Canvas::LoadedPDFInfo> pdfInfo;
But even when declaring variables (or type-casting) using actual struct types, you still do not need the struct prefix. C needs that, C++ does not.
Second, your segfault is happening because you have merely declared the pdfInfo variable, but it is not actually pointing at a valid LoadedPDFInfo object, so using the -> operator is not a valid operation. Just like a regular pointer, std::unique_ptr (and std::auto_ptr, and std::shared_ptr) have to point at something in order to access that something's members. For example:
std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo(new Canvas::LoadedPDFInfo);

Is it possible to create temp structs/classes with initializer lists? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
It looks like a very basic question that should have an explicit googlable answer, but I wasn't able to find it so far. It seems like the answer is no, but I wanted to make sure.
What I mean is comething like this:
struct A { int a,b; };
void foo(const A&); // just some function
foo(A{1,2}); // doesn't work, but it was a long shot
foo(A({1,2})); // looks like it could have worked, but it doesn't
foo(A(std::initializer_list<int>({1,2}))); // ugly and doesn't work either anyway
Update:
option 2 actually works (as well as an even a simpler foo({1,2})), but only in vs2013, not in 2012.
Update #2: come on, was it necessary to downvote it so hard? It's still useful to know that it only works starting with vs2013.
Even easier than that. Behold the power of uniform initialization:
foo({1,2});