#include <functional>
#include <map>
#include <string>
#include <iostream>
class X
{
public:
X()
{
std::cout << "Ctor\n";
}
private:
typedef std::map<std::string, std::function<void()>> ValidatorType;
const ValidatorType m_validators = ValidatorType
{
{
"some-string",
[]()
{
// validation code
std::cout << "Validating...\n";
}
}
};
};
int main()
{
std::cout << "Start...\n";
X x;
std::cout << "Complete...\n";
return 0;
}
The above code builds and runs successfully in debug and release mode on OS X using Xcode 7.2.1 and Clang 7.0.2.
It also builds and runs successfully in release mode on Windows 7 using Visual Studio Express 2013 for Windows Desktop.
However, it crashes when run in debug mode on Windows. An access violation occurs before the constructor finishes executing. The console output is as follows:
Start...
Ctor
If the initialisation of m_validators is moved to the constructor initialiser list then the error goes away.
Could this be a compiler bug or is there something wrong with the declaration?
I tried building your code with VS2015, and it runs fine in debug build. I got this output:
Start...
Ctor
Complete...
without any "crash".
It may be a compiler bug with VS2013. You may want to upgrade to a new C++ compiler.
Related
The following code crashes deep inside the standard library:
#include <set>
#include <vector>
struct Parent
{
std::set<int> v;
};
struct Child : public Parent
{
Child() = default;
};
int main()
{
std::vector<Child> v{ {} };
}
but if the constructor is explicitly declared (without = default), it doesn't crash. I don't understand why.
I'm using VS2019.
I looked up into the code and it really caused to happen in Visual Studio 16.7.7. Now it seems to be working fine in both in VS 16.8.3 and g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 (tried with -std=c++14), it was a bug which seems to be fixed by Microsoft team in the recent update.
I keep getting terminate called for anything I throw using GCC 9.2, even if it is caught.
terminate called after throwing an instance of 'char const*'
terminate called recursively
I have tested -std=c++17, -std=c++14, -std=c++11
Example test:
#include <iostream>
int main()
{
try
{
throw "not found";
}
catch(...)
{
std::cout << "test" << std::endl;
}
return 0;
}
It doesn't fail if i compile using visual studio or on multiple of the online compilers.
example:
https://repl.it/languages/cpp
https://www.onlinegdb.com/online_c++_compiler
I have also tried putting the throw in a function and adding noexcept(false), but this fails as well. Example:
#include <iostream>
void foo() noexcept(false)
{
throw std::runtime_error( "test1" );
}
int main()
{
try
{
foo();
}
catch(...)
{
std::cout << "test2" << std::endl;
}
return 0;
}
Edit:
System info:
I'm using 9-2020-q2-update - arm-linux-none-gnueabihf.
Basically, the setup is Linux x86 as my main computer, cross compiling for ARM Cortex-A processor. The processor i'm testing are Raspberry Pi 4 and BeagleBone Black.
The code compiles correctly and runs fine on the target processor, except when an exception is hit. At which point, it hits terminate for any throw.
I'm using Eclipse as the IDE, using remote debug to upload and step through the code on either of the target processors.
It seems there is a bug or exception handling isn't working on version 9.2 of GCC (ARM only?) compiler.
I tried with version 8.3-2019.03 - arm-linux-gnueabihf - Linux x86 compiler and they are working just fine. No other changes were necessary, other than the compile switch.
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
I expected the following code to compile, but Visual Studio 2013 Update 2 gives me an error, while g++ 4.7 compiles it fine.
using std::vector;
using std::string;
struct Settings
{
vector<string> allowable = { "-t", "--type", "-v", "--verbosity" };
};
VS 2013 compile fails with:
'std::vector<std::string,std::allocator<_Ty>>::vector' : no overloaded function takes 4 arguments
If I change the member as follows then it compiles fine:
vector<string> allowable = vector<string> { "-t", "--type", "-v", "--verbosity" };
I've looked at the proposal linked from Bjarne's FAQ and I've looked at the MSDN page on completed C++11 features in VS 2013 but I'm still confused. Should it compile as is, or am I wrong and must specify the type twice?
The example that you showed is perfectly valid C++, however it doesn't work for VC++2013.
This is a known VC++2013 bug reported since 31/10/2013 and its status is still active.
However, you can surmount it by doing a work-around. As #ildjarn suggested, by simply putting an extra pair of curly braces you force initializer_list<> constructor of the std::vector to be evoked instead of its fill constructor, like the example below:
#include <string>
#include <vector>
#include <iostream>
struct Settings {
std::vector<std::string> allowable = {{"-t", "--type", "-v", "--verbosity"}};
};
int main() {
Settings s;
for (auto i : s.allowable) std::cout << i << " ";
std::cout << std::endl;
}
This is really driving me crazy:
#include <iostream>
#include <vector>
#include <string.h>
#include <thread>
using namespace std;
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
int main() {
thread(test).join();
return 0;
}
The code compiles fine with the -std=c++11 flag to the compiler and the -pthread flag to the linker.
BUT: Eclipse does either know the std::thread or the myvector.begin()->length(), even if the code runs fine eclipse warns me "Method 'length' could not be resolved".
I tried every possible solution in here: Eclipse CDT C++11/C++0x support without any success. This took me so many hours now, what am I doing wrong?!
Is there anybody getting a project setup without problems with this code?
EDIT: Other code example - same problem:
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
Compiles and runs correct, but returns in eclipse: Method 'test' could not be resolved
EDIT:
working versions:
((TestClass)*(testClassVector.begin())).test();
TestClass foo2 = *(testClassVector.begin());
foo2.test();
still not working:
testClassVector.begin()->test();
The last compiles and works like the two above, but eclipse still claims:
Method 'test' could not be resolved
Maybe I'm wrong, but I think your problem don't come from Eclypse. Juste, begin() on a vector return a std::vector<T>::iterator first, this is not a pointer and there is no method length, but you can ask for the vector size with myvector.size(); if this is what you want.
The problem could come from your #include <string.h> that is not the same as #include <string>, string.h is for string operation like strcmp, strstr, etc... juste string will define the std::string object.
I don't have Eclipse set up but the problem appears to be around std::string. Does the problem go away if you remove the threading from the example? (I also changed to #include <string> instead of string.h)
#include <iostream>
#include <vector>
#include <string>
#include <thread>
using namespace std;
#if 0
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
#endif
int main() {
//thread(test).join();
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
return 0;
}
That should hopefully print out 10.
Update from comment:
Does this generate the Eclipse warning?
auto tmp = *(myvector.begin());
std::cout << tmp.length() << std::endl;
What about this?
std::string foo("abc123");
std::cout << foo.length() << std::endl;
I guess one more too:
std::string foo2 = *(myvector.begin());
std::cout << foo2.length() << std::endl;
The solution found:
I downloaded eclipse kepler Kepler
Created a new project and tried to compile this source code (like above):
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
On the first run eclipse told me, thread belongs to the new c++11 standard and I have to add -std=c++11 to the compiler flags. To use thread I also added -pthread to the linker flags. With this steps the code could be compiled, but eclipse marks the thread still as unknown. To fix this I proceeded the following step:
Under C/C++ Build (at project settings), find the Preprocessor Include Path and go to the Providers Tab. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11 to the text box called Command to get compiler specs.
Found here.
Now - unbelievable but true - it works, even without any errors marked by eclipse. The solution is using the (beta) version of eclipse, wich seems to handle this in a better way.
Thanks for all your help!
struct test_struct
{
test_struct() {}
~test_struct() {}
};
#include <vector>
#include <memory>
#include <cstdio>
int main()
{
printf("ctor begin\n");
{
std::vector<std::unique_ptr<test_struct>> test_vec;
const int count = 100000;
for (auto i = 0; i < count; i++) {
test_vec.emplace_back(new test_struct);
}
printf("dtor begin\n");
}
printf("dtor end\n");
}
I'm using VS2010, and found some ridiculous performance issue. The code above works well both in debug and release build (ctrl+f5), but when debugger is attached(f5), dtor call for unique_ptr class is intolerably slow. The result machine code is fairly optimized, so I don't expect that it's compiler issue rather than debugger's, but I don't know how to deal with it. My question is
Is this problem able to be reproduced on your machine?
What's the reason of this behaviour?
Is there any workaround?
The slowdown is caused by memory checking that occurs whenever memory is freed. However, this is a special system-/debugger-level heap, and isn't anything you can control from within your program.
There's a great article on the issue. To summarize: you have to set an environment variable to disable it!
Luckily, you can set project-specific environment variables from the Debugging options in the Project Settings for your project, so that the environment variable is only applied to your program.
I used this simplified program to test:
#include <iostream>
#include <memory>
#include <vector>
int main()
{
std::cout << "ctor begin" << std::endl;
{
std::vector<std::unique_ptr<int>> test_vec;
for (unsigned i = 0; i < 100000; i++)
test_vec.emplace_back(new int);
std::cout << "dtor begin" << std::endl;
}
std::cout << "dtor end" << std::endl;
}
By setting _NO_DEBUG_HEAP=1 as an environment variable (either system-wide, which I won't recommend, or through the Debugging options), the code runs in roughly the same amount of time irrespective of whether or not the debugger is attached.