我正在转换使用POSIX临时文件FILE到C++标准库iostreams的程序.什么是mkstemp的正确替代品?
我想知道是否可以在C++中的unordered_map容器中使用对象引用作为键.
#include <unordered_map>
class Object {
int value;
};
struct object_hash {
inline size_t operator()(const Object& o) const { return 0; }
};
std::unordered_map<Object&, int, object_hash> map;
Run Code Online (Sandbox Code Playgroud)
在尝试编译这个简单的代码片段时,我遇到了一些关于方法重新定义的错误:
使用clang和libc ++
/ usr/include/c ++/v1/unordered_map:352:12:错误:类成员无法重新声明
size_t operator()(const _Cp&__ x)const
使用gcc 4.6和libstdc ++
/usr/include/c++/4.6/bits/hashtable_policy.h:556:5:错误:'std :: __ detail :: _ Map_base <_Key,_Pair,std :: _ Select1st <_Pair>,true,_Hashtable> :: mapped_type&std :: __ detail :: _ Map_base <_Key,_Pair,std :: _ Select1st <_Pair>,true,_Hashtable> :: operator [with _Key = Object&,_ Pair = std :: pair,_Hashtable = std :: …
前段时间,我决定升级到GCC 4.8,以便尽早开始使用某些c ++ 11功能.不过,我有点偏执,并且在几天前的一个项目之前没有真正使用任何新功能(新编译器似乎工作正常,但可能只是因为我没有使用任何新功能.)
在这个新项目中,当我用= std = c ++ 11标志编译时,我没有遇到任何问题.但是,在运行时,我收到错误:
./main: /usr/lib/i386-linux-gnu/libstdc++.so.6: version找不到GLIBCXX_3.4.18'(./ main要求)`
我认为链接到与GCC 4.8相关联的更现代的libstdc ++库存在问题,但我不能在我的生活中弄清楚如何解决这个或适当的库应该在哪里.我记得象征性地将g ++和gcc二进制文件链接到gcc-4.8,它似乎正在工作,因为g++ -v返回:
Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/app/gcc/4.8.0/libexec/gcc/i686-pc-linux-gnu/4.8.0/lto-wrapper Target: i686-pc-linux-gnu Configured with: ./gcc-4.8.0/configure --prefix=/app/gcc/4.8.0 Thread model: posix gcc version 4.8.0 (GCC)
另一个在线线程让我看看ldd程序的输出,它确实告诉我链接的libstdc ++库的目录结构与二进制文件的目录结构不同.但是,我无法libstdc++在后者中找到合适的库,所以我不确定在哪里查看.输出ldd main是:
./main: /usr/lib/i386-linux-gnu/libstdc++.so.6: version找不到GLIBCXX_3.4.18'(./main要求)linux-gate.so.1 =>(0xb7791000)libstdc ++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6(0xb768e000 )libm.so.6 => /lib/i386-linux-gnu/libm.so.6(0xb7662000)libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1(0xb7644000)libc .so.6 => /lib/i386-linux-gnu/libc.so.6(0xb749b000)/lib/ld-linux.so.2(0xb7792000)`
我不确定这到底出了什么问题,我会继续谷歌搜索并寻找答案,但是你们提供的任何帮助都将不胜感激.如果有任何问题不清楚或者我忘了一些信息,请告诉我,我会尝试添加它.非常感谢!
我想在此之后建立Clang:http://clang.llvm.org/get_started.html
在第6步,该命令../llvm/configure运行一系列检查,一个告诉我:
checking whether Clang will select a modern C++ standard library... no
configure: error:
We detected a missing feature in the standard C++ library that was known to be
missing in libstdc++4.6 and implemented in libstdc++4.7. There are numerous
C++11 problems with 4.6's library, and we don't support GCCs or libstdc++ older
than 4.7. You will need to update your system and ensure Clang uses the newer
standard library.
If this error is incorrect …Run Code Online (Sandbox Code Playgroud) 在IOS 64位项目中,我看到了大量的问题,如Apple Mach-O Linker(Id)错误和cryptopp中的未定义符号.问题通常描述为:
Undefined symbols for architecture i386:
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()", referenced from:
cv::gpu::error(char const*, char const*, int, char const*) in opencv2(gpumat.o)
Run Code Online (Sandbox Code Playgroud)
问题通常会减少到混合/匹配-stdlib=libc++(LLVM C++运行时)和-stdlib=libstdc++(GNU C++运行时).LLVM的C++运行时(libc++)具有__1装饰符号,但GNU C++运行时libstdc++ 缺少的__1在其名称符号.它会导致符号显示具有相同名称的链接器问题(如std::string).
__1使用LLVM的libc ++时,符号来自何处?
为什么这个问题不是一个解决gnu命名和llvm命名空间?
这是一个相关的问题:libc ++ - 停止std重命名为std :: __ 1?.但它有点忽略了重命名不会发生的重点.
请考虑以下最小示例:
// main.cpp
#include <random>
int main(int, char **)
{
std::seed_seq seed1{1337, 42};
std::seed_seq seed2(seed1);
std::seed_seq seed3 = seed2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据C++标准,这不应该编译,因为std::seed_seq既不是可复制的,也不是可复制的.
然而,这个编译罚款既g++ 4.9和clang 3.4
g++-4.9 -std=c++11 -Wall main.cpp
clang++ -std=c++11 -Wall main.cpp
Run Code Online (Sandbox Code Playgroud)
android ndk的llvm-libc++实现似乎遵循了"不可复制"的属性seed_seq.哪个可以在源头确认
android-ndk-r10d/sources/cxx-stl/llvm-libc++/libcxx/include/random:3553
Run Code Online (Sandbox Code Playgroud)
或者通过编译最小的例子
${NDK_HOME}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ \
-std=c++11 -c -Wall \
-I${NDK_HOME}/sources/cxx-stl/llvm-libc++/libcxx/include \
-I${NDK_HOME}/sources/cxx-stl/llvm-libc++/../llvm-libc++abi/libcxxabi/include \
-I${NDK_HOME}/sources/cxx-stl/llvm-libc++/../../android/support/include \
-isystem ${NDK_HOME}/platforms/android-18/arch-arm/usr/include \
main.cpp
Run Code Online (Sandbox Code Playgroud)
我之前使用过这个(不知道我的不符合代码)来存储种子的副本以用于记录目的.*
我想知道:
为什么seed_seq不可复制?
这是我第一次遇到g++并且clang不符合标准.是否有意识地决定偏离标准,或者这是一个实现错误?这有多普遍?我想了解更多.
*我意识到我在想seed_seq错,如果我只对 …
我抓我的头std::optional,而根据该文档,不应该有一个constexpr赋值运算符.
但是,当我在gcc-8.1中尝试这个片段时,它编译并正常工作:
constexpr std::optional<int> foo() {
std::optional<int> bar = 3;
bar = 1337;
return bar;
}
constexpr auto z = foo();
Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?
我想用stdlibc ++编译Mac OS X 10.9的boost.我运行以下命令:
./b2 threading=multi link=static runtime-link=static cxxflags="-stdlib=libstdc++" linkflags="-stdlib=libstdc++"
Run Code Online (Sandbox Code Playgroud)
构建成功完成; 但是,我的应用程序构建在链接时失败,当它找不到符号时,std :: __ 1 :: locale :: use_facet,std :: __ 1 :: basic_string等.我相信__1的相关细节.
我的问题是,如何使用stdlibc ++编译OSX 64b平台的boost?
更多信息:
我在编译期间注意到以下日志:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib:file:bin.v2/libs/filesystem/build/clang-darwin-4.2.1/release/link-static/runtime -link-static/threading-multi/libboost_filesystem.a(windows_file_codecvt.o)没有符号
我对 iostream 和 bits/stdc++.h 之间的差异感到困惑?在竞争性编程中可以使用bits/stdc++.h吗?或者有什么后果吗?
升级到 Ubuntu 22.04 (amd64) 时,我注意到以下代码开始给出结果 1.4375,而不是预期值 1472:
#include <charconv>
#include <iostream>
#include <string_view>
int main()
{
std::string_view src{"1.7P10"};
double value;
auto result = std::from_chars(src.data(), src.data() + src.size(), value, std::chars_format::hex);
std::cout << value << '\n';
}
Run Code Online (Sandbox Code Playgroud)
P如果我将源中的字符更改为小写,我会得到预期的结果p。大写和小写都适用于std::strtod.
应该std::from_chars会因大写指数字符而失败,或者这是 g++/libstdc++ 中的错误?
用于编译的命令行:
g++ -std=c++17 test.cpp
Run Code Online (Sandbox Code Playgroud)
(优化级别似乎没有效果)
输出g++ --version:
g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; …Run Code Online (Sandbox Code Playgroud)