我有一些简单的代码
#include<iterator>
int main() {
int y[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
auto a = std::begin(y);
std::cout << *a << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
1按预期打印出来。
但是如果我这样做:
void checkNested(int val [10]) {
auto a = std::begin(val);
std::cout << *a << std::endl;
}
int main() {
int y[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
checkNested(y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
clang++我从和得到编译失败g++。具体来说clang++我得到:
auto a = std::begin(input);
^~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/initializer_list:89:5: …Run Code Online (Sandbox Code Playgroud) scons我目前正在尝试编译一个我一直在使用 Clang 编写的小程序,并且在使用构建系统进行编译时出现以下错误:
/usr/bin/clang++ -o src/PluGen/main.o -c -std=c++17 -fPIC -I. -O2 -fno-strict-aliasing -fpermissive -fopenmp --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -fpermissive -fPIC -I. -O2 -flto -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -DHAVE_PYTHON -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DLARGE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_PERL -DREENTRANT -DHAVE_R -I/usr/include -I/usr/local/include -Isrc -I/usr/include/python3.10 -I/usr/lib/perl5/5.36/core_perl/CORE -I/usr/include/R -I/usr/lib/R/library/Rcpp/include -I/usr/lib/R/library/RInside/include -I/usr/lib/R/site-library/RInside/include -I/usr/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/library/Rcpp/include -I/usr/local/lib/R/library/RInside/include -I/usr/local/lib/R/site-library/RInside/include -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RInside/lib src/PluGen/main.cxx
...
/usr/bin/clang++ -o PluGen/plugen -rdynamic -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.36/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now src/PluGen/main.o src/PluGen/PluginGenerator.o -L/lib -L/usr/lib -L/usr/local/lib -L/usr/lib/perl5/5.36/core_perl/CORE -L/usr/lib/R/lib -L/usr/lib/R/library/RInside/lib -L/usr/lib/R/site-library/RInside/lib -L/usr/local/lib/R/library/RInside/lib -L/usr/local/lib/R/site-library/RInside/lib -Llib -lc -lc++ -lstdc++fs
src/PluGen/main.o: file not …Run Code Online (Sandbox Code Playgroud) 我在我们的 C++ iOS 应用程序中发现了一个严重的错误,我怀疑这是由基于 ARM 的 Apple Clang 上的编译器错误引起的。
我能够在 Mac M1 机器上的 MRE 中重现该错误。
#include <cstdio>
int main(int argc, const char** argv)
{
int total = 0;
for(double a=1000; a<10000; a*=1.1)
{
unsigned char d = a / 0.1;
total += d;
}
printf("Total: %d\n", total);
}
Run Code Online (Sandbox Code Playgroud)
在没有优化的情况下编译,测试程序总是产生相同的输出:
% ./a.out
Total: 3237
% ./a.out
Total: 3237
% ./a.out
Total: 3237
Run Code Online (Sandbox Code Playgroud)
然而,当进行优化编译时,结果数字似乎是随机的:
% clang -O3 test.cpp
% ./a.out
Total: 74841976
% ./a.out
Total: 71057272
% ./a.out
Total: 69828472
Run Code Online (Sandbox Code Playgroud)
Apple Clang …
我试着写一个非常简单的进度条类.我将所有内容放入progressBar.h中,并创建了一个最小的测试用例.我在Ubuntu 14.04上使用clang ++ 3.4运行它.我包括-std = c ++ 11标志.测试用例创建了一个新的ProgressBar对象并调用成员函数displayProgress(double).我包括在ProgressBar.h中.
这是我的代码:
#include <ostream>
#include <string>
class ProgressBar
{
private:
std::ostream& out;
int width;
public:
ProgressBar(std::ostream& out, int _width = 80)?out(out) {
width = _width - 9;
}
void displayProgress(double percentage) {
out << "\r[" << fixed << setprecision(2) << percentage * 100 << "%]";
for (int i = 0; i < width * percentage; ++i) out << "=";
if (percentage != 1) out << ">";
else out << "O";
}
};
Run Code Online (Sandbox Code Playgroud)
这是错误消息: …
可以使用sph_legendreor调用tr1并编译它gcc-5gcc-6
#include<tr1/cmath>
int main()
{
std::tr1::sph_legendre(1,1,0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果我尝试将其编译为clang++:
clang++ -stdlib=libstdc++ legendre.cpp -o legendre
我明白了
错误:命名空间“std::tr1”中没有名为“sph_legendre”的成员
在第 40.3 节中,C++PP(第 4 版)指出:
“对于特殊数学函数有一个单独的 ISO 标准 [C++Math,2010]。实现可以将这些函数添加到 cmath”
我怎样才能编译这些特殊函数clang++?
今天,我得到不同寻常的错误时使用clang++和ifstream:
*** stack smashing detected ***: <unknown> terminated
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
uint32_t Lod::getGraphicsOffset(std::ifstream *file, int graphicsNumber) {
uint32_t filesAmount = 0;
fileStream.seekg(76 + graphicsNumber*32);
fileStream.read(reinterpret_cast<char*>(&filesAmount), 16);
return filesAmount;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我只改变fileAmount代码中的位置时,它是有效的:
uint32_t Lod::getGraphicsOffset(std::ifstream *file, int graphicsNumber) {
fileStream.seekg(76 + graphicsNumber*32);
uint32_t filesAmount = 0;
fileStream.read(reinterpret_cast<char*>(&filesAmount), 16);
return filesAmount;
}
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?它的原因是什么?也许是一些UB?
-c标志在以下命令中起什么作用?
clang++ -std=c++11 -g -Wall -stdlib=libc++ -isystem testing/gtest-1.7.0/include -Itesting/gtest-1.7.0 -pthread -c testing/gtest-1.7.0/src/gtest-all.cc
Run Code Online (Sandbox Code Playgroud)
我在文档(http://clang.llvm.org/docs/UsersManual.html)以及帮助消息(clang -cc1 --help)中查找了该标志...似乎找不到答案。
当我编写C++代码并通过clang ++编译器编译它时,
error: expected expression
template <typename T>
^
Run Code Online (Sandbox Code Playgroud)
有代表.
为什么会出现此错误,如何解决?
#include<iostream>
using namespace std;
int main() {
template <typename T>
T sum(T a, T b) {
return a+b;
}
cout <<"Sum = " << sum( 2.1, 7.9 ) << endl;
return 1;
}
Run Code Online (Sandbox Code Playgroud)