考虑一下片段:
#include <unordered_map>
void foo(const std::unordered_map<int,int> &) {}
int main()
{
foo({});
}
Run Code Online (Sandbox Code Playgroud)
这与GCC 4.9.2失败并显示以下消息:
map2.cpp:7:19: error: converting to ‘const std::unordered_map<int, int>’ from initializer list would use explicit constructor ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Key = int; _Tp = int; _Hash = std::hash<int>; _Pred = std::equal_to<int>; _Alloc = std::allocator<std::pair<const int, int> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hasher = std::hash<int>; std::unordered_map<_Key, …Run Code Online (Sandbox Code Playgroud) 以下片段:
#include <memory>
#include <utility>
namespace foo
{
template <typename T>
void swap(T& a, T& b)
{
T tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}
struct bar { };
}
void baz()
{
std::unique_ptr<foo::bar> ptr;
ptr.reset();
}
Run Code Online (Sandbox Code Playgroud)
不为我编译:
$ g++ -std=c++11 -c foo.cpp
In file included from /usr/include/c++/5.3.0/memory:81:0,
from foo.cpp:1:
/usr/include/c++/5.3.0/bits/unique_ptr.h: In instantiation of ‘void std::unique_ptr<_Tp, _Dp>::reset(std::unique_ptr<_Tp, _Dp>::pointer) [with _Tp = foo::bar; _Dp = std::default_delete<foo::bar>; std::unique_ptr<_Tp, _Dp>::pointer = foo::bar*]’:
foo.cpp:20:15: required from here
/usr/include/c++/5.3.0/bits/unique_ptr.h:342:6: error: call of …Run Code Online (Sandbox Code Playgroud) 我想我从C++标准库遇到了std :: poisson_distribution的错误行为.
以下C++代码(文件poisson_test.cc)用于生成泊松分布数:
#include <array>
#include <cmath>
#include <iostream>
#include <random>
int main() {
// The problem turned out to be independent on the engine
std::mt19937_64 engine;
// Set fixed seed for easy reproducibility
// The problem turned out to be independent on seed
engine.seed(1);
std::poisson_distribution<int> distribution(157.17);
for (int i = 0; i < 1E8; i++) {
const int number = distribution(engine);
std::cout << number << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我按如下方式编译此代码:
clang++ -o poisson_test -std=c++11 poisson_test.cc …Run Code Online (Sandbox Code Playgroud) 我正在创建一个无中生有的新项目,用于测试目的,在新的ADT安装(Ubuntu Gnome 14.04 LTS,x86_64 CPU)上将所有参数保留为默认值(我没有进行任何代码更改),但我有以下内容Eclipse控制台中的错误:
[2014-06-11 09:03:10 - Kronos] /home/erwan/Applications/ADT/adt-bundle-linux-x86_64-20140321/sdk/build-tools/19.1.0/aapt: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
这是我已经尝试过的:
- >我试图通过Ubuntu软件库(重新)安装ia32-libs,libstdc ++和libstdc ++ 6:没有变化
- >检查更新(对于Eclipse和SDK):没有变化
- >重新安装所有Android Build工具:无变化
- >重新安装亚行:没有变化
我正在尝试使用ld而不是g ++链接C++的输出.我只是这样做是为了学习如何做,而不是为了实际目的,所以请不要建议只用g ++来做.
看看这个问题,这个人在运行ld命令时会得到同样的错误:
$ ld test.o -o test.out
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8
test.o: In function `main':
test.cpp:(.text+0x1c): undefined reference to `strcasecmp'
test.cpp:(.text+0x23): undefined reference to `std::cout'
test.cpp:(.text+0x28): undefined reference to `std::ostream::operator<<(int)'
test.cpp:(.text+0x2d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x35): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
test.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x75): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x7a): undefined reference to `__dso_handle'
test.cpp:(.text+0x84): undefined reference to `std::ios_base::Init::~Init()'
test.cpp:(.text+0x89): undefined …Run Code Online (Sandbox Code Playgroud) shared_ptr我最近在 lambda 中捕获 a 时在程序中遇到了一个奇怪的双重释放错误。我能够将其减少为以下最小示例:
#include <memory>
#include <functional>
struct foo {
std::function<void(void)> fun;
};
foo& get() {
auto f = std::make_shared<foo>();
// Create a circular reference by capturing the shared pointer by value
f->fun = [f]() {};
return *f;
}
int main(void) {
get().fun = nullptr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用 GCC 12.2.0 和地址清理程序编译并运行它,会在 中产生双重释放std::function:
$ g++ -fsanitize=address -g -Wall -Wextra -o main main.cpp && ./main
=================================================================
==2401674==ERROR: AddressSanitizer: attempting double-free on 0x602000000010 in thread T0:
#0 …Run Code Online (Sandbox Code Playgroud) 在阅读了DLL和EXE之间传递空对象的问题之后std::string,我担心用于构建我的gcc/libstdc ++的configure选项.更具体我想知道是否--enable-fully-dynamic-string在期间使用./configure.
我在Windows XP上使用MinGW 4.4.0.
有人知道用于构建此版本的配置吗?
是否有一般方法可以为任何GNU gcc安装找到此信息?gcc手册没有给我这个主题的暗示.
感谢您的输入!
我使用新的c ++ 11 std::thread接口遇到了问题.
我无法弄清楚如何std::ostream将对a的引用传递给线程将执行的函数.
这是传递整数的示例(在gcc 4.6下按预期编译和工作):
void foo(int &i) {
/** do something with i **/
std::cout << i << std::endl;
}
int k = 10;
std::thread t(foo, k);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试传递一个ostream时,它无法编译:
void foo(std::ostream &os) {
/** do something with os **/
os << "This should be printed to os" << std::endl;
}
std::thread t(foo, std::cout);
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,还是根本不可能?
注意:从编译错误看来它似乎来自一个已删除的构造函数...
我需要帮助.我有一个使用stdc ++的框架,比如std:string.现在,当我为IOS7创建新的应用程序时,由于stdc ++ lib的问题,链接此框架存在问题:
架构armv7的未定义符号"std :: basic_string,std :: allocator> :: _ Rep :: _ S_empty_rep_storage",引自...
我发现一些奇怪的事情,当我在这个应用程序中将Deplyment目标更改为ios6时,一切正常.使用ios7,我看到错误.
我已经在其他链接器标志中设置了标志:-lstdc ++
知道自己做错了什么吗?
我希望使用-fsanitize=memoryclang中的标志来分析如下的程序:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void writeToFile(){
ofstream o;
o.open("dum");
o<<"test"<<endl; //The error is here.
//It does not matter if the file is opened this way,
//or with o("dum");
o.close();
}
int main(){
writeToFile();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这个程序是正确的,但是当我使用clang++ san.cpp -fsanitize=memory它时失败(在运行时):
UMR in __interceptor_write at offset 0 inside [0x64800000e000, +5)
==9685== WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x7f48d0899ae5 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x7bae5)
#1 0x7f48d08d1787 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb3787)
#2 0x7f48d08d21e2 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb41e2)
#3 0x7f48d08cfd1e (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb1d1e)
#4 0x7f48d08b1f2d (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x93f2d)
#5 0x7f48d16d60f5 in writeToFile() /home/daniel/programming/test/santest.cpp:10 …Run Code Online (Sandbox Code Playgroud)