我尝试学习Grand Central Dispatch(GCD)并使用以下代码进行测试:
使用GCD:
#include <dispatch/dispatch.h>
#include <vector>
#include <cstdlib>
#include <iostream>
int main(int argc, char *argv[])
{
const int N = atoi(argv[1]);
__block std::vector<int> a(N, 0);
dispatch_apply(N,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(size_t i)
{
a[i] = i;
#ifdef DEBUG
if ( i % atoi(argv[2]) == 0)
std::cout << a[i] << std::endl;
#endif
});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没有GCD:
#include <vector>
#include <cstdlib>
#include <iostream>
int main(int argc, char *argv[])
{
const int N = atoi(argv[1]);
std::vector<int> a(N, 0);
for (int i = …Run Code Online (Sandbox Code Playgroud) Sales_data.h
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <string>
class Sales_data {
friend std::istream &read(std::istream &in, Sales_data &data);
friend std::ostream &print(std::ostream &out, const Sales_data &data);
public:
Sales_data() = default;
//Sales_data(std::string _bookNo="", unsigned _units_sold = 0, double _revenue = 0)
//:bookNo(_bookNo), units_sold(_units_sold), revenue(_revenue) {}
std::string isbn() { return bookNo; }
Sales_data& combine(const Sales_data&);
double avg_price() const;
private:
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0;
};
Sales_data &Sales_data::combine(const Sales_data& data) {
units_sold += data.units_sold;
revenue += data.revenue;
return *this;
}
std::istream …Run Code Online (Sandbox Code Playgroud) 我试图使用看起来像这样的代码编译项目
#include <tuple>
#include <utility>
struct Foo
{
};
template <typename... Args>
void start(Args&&... args) {
auto x = [args = std::make_tuple(std::forward<Args>(args)...)] () mutable {
auto y = [args] () mutable {
auto z = [] (Args&&... args) {
return new Foo(std::forward<Args>(args)...);
};
};
};
}
int main()
{
start(Foo{});
}
Run Code Online (Sandbox Code Playgroud)
似乎在GCC 4.9.1中编译良好,但在Clang 3.4,3.5,3.6中没有编译.错误消息是
错误:变量'args'不能在没有指定capture-default的lambda中隐式捕获
这是编译器错误吗?如果是这样,是否有任何解决方法让它在Clang上编译?
我的评论以反斜杠结尾.就像是
...
// use \
..
Run Code Online (Sandbox Code Playgroud)
Clang(++)警告我,这是多行评论
warning: multi-line // comment [-Wcomment]
// use \
^
Run Code Online (Sandbox Code Playgroud)
所以我尝试在最后添加一些空格,但没有帮助.我可以以某种方式逃避反斜杠吗?
我试图在clang上编译以下代码,但它失败并出现以下错误:
error: no member named 'operator<' in the global namespace
Run Code Online (Sandbox Code Playgroud)
我尝试使用/ Za编译visual studio代码以切换到标准一致性,但它似乎仍然接受它.请赐教.
struct A
{
int m_test;
A(int test)
: m_test(test)
{
}
friend bool operator<(A left, A right);
};
int main()
{
typedef bool(*TCompare)(A,A);
TCompare compare = &::operator<;
compare(9,7);
}
Run Code Online (Sandbox Code Playgroud)
VC++输出:https://godbolt.org/g/LAz56n
Clang输出:https://godbolt.org/g/zC2InO
考虑以下简单程序:
#include <iostream>
void foo() { }
int main() {
std::cout<<static_cast<void*>(foo);
}
Run Code Online (Sandbox Code Playgroud)
它编译罚款VC++,但g++与clang++给编译错误.
在这里看现场演示(VC++)
在这里看现场演示(clang++)
在这里看现场演示(g++)
诊断由g++&给出clang++:
source_file.cpp: In function ‘int main()’:
source_file.cpp:4:38: error: invalid static_cast from type ‘void()’ to type ‘void*’
std::cout<<static_cast<void*>(foo);
^
Run Code Online (Sandbox Code Playgroud)
那么,问题是根据C++标准,哪个编译器就在这里?我认为,行为g++和clang++正确这里.我知道我应该reinterpret_cast在这里使用而不是static_cast.这是VC++编译器中的错误吗?如果答案取决于C++的具体标准,那么我也很想知道它.
在编译我的项目时,我得到警告匿名联合中声明的匿名类型是扩展名[-Wnested-anon-types].我的代码包含这个联合:
union
{
uint32_t m_bits; // All bits
struct
{
uint32_t significand : 23;
uint32_t exponent : 8;
uint32_t sign : 1;
} IEEE;
};
Run Code Online (Sandbox Code Playgroud)
至于网站上的其他答案已经说过,如果我IEEE从结构中省略了名称,我只会期望这个警告.但目前结构不应该是匿名类型?
我试图触及C++ 17的功能,我选择了clang.这是我的代码的简化示例,无法通过clang编译:
#include <iostream>
#include <limits>
template<
typename T,
template<typename T_> typename Final>
class Base
{
public:
decltype(auto) Foo() const noexcept
{
using TFinal = Final<T>;
auto& _this = static_cast<const TFinal&>(*this);
return _this.Bar<true>();
}
};
template<typename T>
class Derived :
public Base<T, ::Derived>
{
public:
template<bool min>
T Bar() const noexcept
{
return min ?
std::numeric_limits<T>::lowest() :
std::numeric_limits<T>::max();
}
};
int main()
{
Derived<int> instance;
auto result = instance.Foo();
std::cout << result << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它失败了:
return _this.Bar<true>(); …Run Code Online (Sandbox Code Playgroud) 我试图运行Ale作为我的linter,然后使用clang-check来lint我的代码.
$ clang-check FeatureManager.h
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "FeatureManager.h"
No compilation database found in /home/babbleshack/ or any parent directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
/home/babbleshack/FeatureManager.h:6:10: fatal error: 'unordered_map' file not found
#include <unordered_map>
^~~~~~~~~~~~~~~
1 error generated.
Error while processing /home/babbleshack/FeatureManager.h.
Run Code Online (Sandbox Code Playgroud)
而使用clang ++进行编译只会返回一个警告.
$ clang++ -std=c++11 -Wall FeatureManager.cxx FeatureManager.h
clang-5.0: warning: treating 'c-header' input as 'c++-header' when in C++ mode, …Run Code Online (Sandbox Code Playgroud) 当我使用clang ++ 5.0版编译以下程序时,结果是
错误:函数的初始值设定项看起来不像纯说明符
extern void print(void *ptr);
#define NULL __null
class IInterface
{
public:
virtual void method1() = NULL;
};
int main()
{
void *ptr = NULL;
print(ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
似乎__null不被clang支持吗?但是stackoverflow中的一些帖子建议clang支持__null。如果是这样,我为什么会收到此错误。有人可以建议这里发生了什么吗?
clang++ ×10
c++ ×9
c++11 ×2
c++14 ×2
visual-c++ ×2
c++17 ×1
clang ×1
comments ×1
escaping ×1
friend ×1
g++ ×1
llvm-clang ×1
static-cast ×1
templates ×1