My question is related to Prasoon's question about non POD types and value initialization.
I tried the following code on online compilers like Ideone and Codepad but the executables gave runtime error on both the sites.
#include <iostream>
#include <cassert>
struct Struct {
std::string String;
int Int;
bool k;
};
struct InStruct:Struct
{
InStruct():Struct(){}
};
int main()
{
InStruct i;
assert ( i.Int == 0);
std::cout << "Hello";
}
Ideone Output here
Codepad Output here
Does that mean neither of them support C++03 value initialization feature?
Does that mean neither of them support C++03 value initialization feature?
Yes.
Prior to version 4.4, GCC did not completely support value initialization (the Boost GCC compatibility header explains this and has links to the relevant GCC defect reports; see line 77).
If your code needs to be portable, you should be very careful relying on value initialization; GCC did not support it fully until recently and Visual C++ does not fully support it even in its latest version, Visual C++ 2010.
The declaration
InStruct i;
does not invoke value initialization
$8.5.3/10 - "An object whose
initializer is an empty set of
parentheses, i.e., (), shall be
value-initialized."
If you want to value-initialize, you would require an expression something like
assert(InStruct().Int == 0);
Try it now! - Ideone supports GCC-4.5.1
Related
This MCVE works fine in Visual Studio.
#include <experimental/generator>
#include <iostream>
std::experimental::generator<int> f() { for (int i = 0; i < 10; ++i) co_yield i; }
int main ()
{
for (int i : f())
std::cout << i << ' ';
return 0;
}
but in g++10, which is listed as having full support or C++20's coroutines, it does not.
(Taking out experimental doesn't help.)
I am compiling thus: g++ -g -std=c++2a -fcoroutines -c main.cpp.
It complains that there is no include file generator, and if I take out the #include, that generator is not a part of std:: or is not defined. I suppose there's another name for it in the new standard? Or if not, what do I do instead to get a coroutine that uses co_yield?
Nothing in GCC's status list alongside its coroutine support says it supports anything other than p0912r5, which does not provide std::generator, experimentally or otherwise.
I recall that VS added <experimental/generator> a few years ago; I guess GCC never did.
If it's currently proposed for inclusion in C++, and you can find the relevant proposal, perhaps you can track its support status. But honestly, for now, you'd be better off writing your own that works until it becomes part of some actual standard.
tl;dr: Though it is a coroutine, this feature is not part of the Coroutines TS.
If you need a generator for g++11 and above, copy paste the one here:
https://en.cppreference.com/w/cpp/coroutine/coroutine_handle
Please considere the following code-snippet:
#include <iostream>
void print(auto arg) {
std::cout << arg << std::endl;
}
int main() {
print("Hi");
return 0;
}
As you can see here (https://godbolt.org/z/2GSrXs) using GCC the code compiles and runs fine.
As you can see here (https://godbolt.org/z/rtR6w9) using Visual C++ the code does not compile and results in the error message:
<source>(3): error C3533: a parameter cannot have a type that contains 'auto'.
It seems to me that this feature has not jet been implementet in Visual C++?
Additionally I was not able to find the compiler feature in the following list:
https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019. So I am not sure if the Microsoft developer have the feature on their ToDo List?
Functions receiving auto is a C++20 feature. It must be enabled with /std:latest. However, MSVC only has partial support for this syntax at this time, so not all declaration using this feature will work.
GCC has supported auto in function parameters since early C++14 days as an extension.
You can read more about this C++20 feature reading P1141R2: Yet another approach for constrained declarations
Remaining in C++14 standard, you can use the following lambda instead of your function:
auto print = [](auto arg) {
std::cout << arg << std::endl;
};
This MCVE works fine in Visual Studio.
#include <experimental/generator>
#include <iostream>
std::experimental::generator<int> f() { for (int i = 0; i < 10; ++i) co_yield i; }
int main ()
{
for (int i : f())
std::cout << i << ' ';
return 0;
}
but in g++10, which is listed as having full support or C++20's coroutines, it does not.
(Taking out experimental doesn't help.)
I am compiling thus: g++ -g -std=c++2a -fcoroutines -c main.cpp.
It complains that there is no include file generator, and if I take out the #include, that generator is not a part of std:: or is not defined. I suppose there's another name for it in the new standard? Or if not, what do I do instead to get a coroutine that uses co_yield?
Nothing in GCC's status list alongside its coroutine support says it supports anything other than p0912r5, which does not provide std::generator, experimentally or otherwise.
I recall that VS added <experimental/generator> a few years ago; I guess GCC never did.
If it's currently proposed for inclusion in C++, and you can find the relevant proposal, perhaps you can track its support status. But honestly, for now, you'd be better off writing your own that works until it becomes part of some actual standard.
tl;dr: Though it is a coroutine, this feature is not part of the Coroutines TS.
If you need a generator for g++11 and above, copy paste the one here:
https://en.cppreference.com/w/cpp/coroutine/coroutine_handle
The following snippet will compile in GCC 8+, but fails to compile in GCC 7.
template <typename... THINGS>
struct A
{
explicit A(THINGS *... things)
{
(..., [thing = things](){}());
}
};
int main()
{
int thing;
const auto thingy = A{&thing};
}
godbolt
The stated failure is that the parameter pack isn't being expanded: parameter packs not expanded with '...'.
Checking the GCC standards compliance page, fold expressions should be supported in GCC 7.
Is there another flag i need besides std=c++17? (i didn't see one)
Is the standard not yet completely implemented? (i didn't see anything indicating that)
Can i make this work, or is this just a GCC 7 bug i'm going to have to work around?
This is a GCC bug, originally reported in version 8.01, fixed in version 8.2. It seems that the bug also occurs when fold-expressions are not used (the C++11-era "expander trick" mentioned by NathanOliver doesn't work either), so you'll have to use a longer workaround that doesn't require expanding a template parameter pack that is inside a lambda capture. For example:
template <typename THING>
void do_it(THING* thing) {
[thing]{}();
}
explicit A(THINGS *... things)
{
(..., do_it(things));
}
This is a GCC bug. Found the tracked issue, fixed in 8.2.
I'm new to BOOST_AUTO and here is a basic question.
I defined my own class and used it with BOOST_AUTO. According to the tutorial:
If your define your own type, the Typeof Library cannot handle it
unless you let it know about this type. You tell the Typeof Library
about a type (or template) by the means of "registering" this
type/template.
But my code below doesn't register anything and runs correctly in VC++2010. Is this registration really necessary. Did I do anything wrong?
#include <boost/typeof/typeof.hpp>
using namespace std;
namespace NS1 {
struct X {
X(const X& x){s=x.s+1;}
X(){s=3;}
int s;
};
}
int _tmain(int argc, _TCHAR* argv[])
{
NS1::X x;
BOOST_AUTO(y,x);
cout << y.s << endl;
return 0;
}
output:4
from the docs:
The BOOST_AUTO macro emulates the proposed auto keyword in C++.
looking into the code it seems they replace BOOST_AUTO to BOOST_TYPEOF.
typeof seems to be the old extension name of decltype in GCC compilers.
(See this SO question). The code of the boost typeof library doesn't use the auto keyword apparantly for msvc (See here).
It seems that at the time of writing this library, there were some compilers who did support the auto keyword while others did not. If you want to compile the code on architectures that don't support this feature, the registration is necessary.
In any case, since C++11 is released for a while by now. Using this library if you compiler already supports the auto keyword should be considered bad practise, because this library only exists to emulate the proposed feature.
Except of course you have to compile to C++03 for some reason.