这样做是否有任何反指示?或者是否明确规定了行为?
#pragma omp parallel for
for(auto x : stl_container)
{
...
}
Run Code Online (Sandbox Code Playgroud)
因为看起来OpenMP规范只对c ++ 98有效,但我想由于C++ 11线程可能存在更多的不兼容性,这里没有使用它们.我还是想确定一下.
以下代码使用 Clang 编译并运行(在 13、14 和当前 git head 上测试),但不使用 GCC。
struct foo {
int field<0, 1, int, 3>;
};
Run Code Online (Sandbox Code Playgroud)
但我不明白它在声明什么:这个字段是什么?
int field<0, 1, int, 3>;
Run Code Online (Sandbox Code Playgroud)
我可以在field<>
模板中放入我想要的任何内容(如果它甚至是模板?),例如field<0, 1, int, 3>
编译和运行。但之后我无法访问它。
我有以下案例可以使用std::enable_if
:
template<typename T,
typename std::enable_if<std::is_same<int, T>::value>::type* = nullptr>
void f() { }
template<typename T,
typename std::enable_if<std::is_same<double, T>::value>::type* = nullptr>
void f() { }
Run Code Online (Sandbox Code Playgroud)
现在,我在cppreference中看到了新的语法,在我看来更加清晰: typename = std::enable_if_t<std::is_same<int, T>::value>>
我想移植我的代码:
template<typename T,
typename = std::enable_if_t<std::is_same<int, T>::value>>
void g() { }
template<typename T,
typename = std::enable_if_t<std::is_same<double, T>::value>>
void g() { }
Run Code Online (Sandbox Code Playgroud)
但现在海湾合作委员会(5.2)抱怨:
error: redefinition of 'template<class T, class> void g()'
void g() { }
Run Code Online (Sandbox Code Playgroud)
为什么会这样 ?如果可能,我该怎么做才能在这种情况下使用新的,更简洁的语法?
以下代码无法在最近的编译器上构建(g ++ - 5.3,clang ++ - 3.7).
#include <map>
#include <functional>
#include <experimental/string_view>
void f()
{
using namespace std;
using namespace std::experimental;
map<string, int> m;
string s = "foo";
string_view sv(s);
m.find(sv);
}
Run Code Online (Sandbox Code Playgroud)
clang返回的错误:
error: no matching member function for call to 'find'
m.find(sv);
~~^~~~
Run Code Online (Sandbox Code Playgroud)
但是不find
应该能够使用类似的类型?Cppreference提到了以下重载:
template< class K > iterator find( const K& x );
发生同样的错误boost::string_ref
.
我有一些相当简单的代码,仍然gcc抱怨(in -O3 -march=native
)关于循环展开:
cannot optimize loop, the loop counter may overflow [-Wunsafe-loop-optimizations]
for(auto& plan : fw)
^
Run Code Online (Sandbox Code Playgroud)
这是我的代码版本(剥离了所有fftw的东西,否则它会很长)
class FFTWManager
{
public:
void setChannels(unsigned int n)
{
fw.resize(n);
bw.resize(n);
//some fftw-specific stuff comes here
}
void forward()
{
for(auto& plan : fw)
fftw_execute(plan);
}
void backward()
{
for(auto& plan : bw)
fftw_execute(plan);
}
private:
std::vector<fftw_plan> fw = {};
std::vector<fftw_plan> bw = {};
};
Run Code Online (Sandbox Code Playgroud)
在我的代码中,向量永远不会超过2的大小.
根据评论编辑:我使用了很多标志.
-pedantic -Wextra -Weffc++ -Wall -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wformat -Wformat=1 -Wformat-nonliteral -Wformat-security …
是否有一种可接受的"标准"方法来格式化C++> = 11中的lambda表达式?特别是当放入通用算法时.
例如 :
1)
auto it = std::find_if(myVec.begin(),
myVec.end(),
[id = 42] (const Element& e)
{ return e.id() == id;});
Run Code Online (Sandbox Code Playgroud)
或者2)
auto it = std::find_if(myVec.begin(),
myVec.end(),
[id = 42]
(const Element& e)
{ return e.id() == id;});
Run Code Online (Sandbox Code Playgroud)
或者3)
auto it = std::find_if(myVec.begin(),
myVec.end(),
[id = 42] (const Element& e)
{
return e.id() == id;
});
Run Code Online (Sandbox Code Playgroud)
或者4)
auto it = std::find_if(myVec.begin(),
myVec.end(),
[id = 42] (const Element& e)
{
return e.id() == id;
});
Run Code Online (Sandbox Code Playgroud)
或者回车,空格,制表符的任何其他组合......注意:我在我的代码中使用Allman样式,所以理想情况下它将"适合相同的样式".
我希望能够做到以下几点:
#include <array>
struct blah { };
template<typename... Args>
constexpr auto foo(Args&&... args)
{
return std::array<blah, sizeof...(Args)>{{ args... }};
}
auto res = foo({}, {});
Run Code Online (Sandbox Code Playgroud)
以下答案并不令人满意:他们只想检查参数包是否为单一类型,但我想在参数中将值正确转换为它(否则它不起作用).
为使用数组,向量,结构等传递给variadic函数或可变参数模板函数的所有参数指定一种类型?
我也不能使用initializer_list,因为我无法计算传递给该array
类型的参数数量.我特别不想打字foo(blah{}, blah{});
.
我的可能性是什么?
我希望能够检测我的函数(或它调用的任何其他函数)是否最终会在我的单元测试中调用某些特定函数(例如,malloc
和free
):我的软件的一小部分具有硬实时要求,并且我想确保没有人添加会在这些函数中偶然触发分配的东西(并让我的CI管道自动检查它).
我知道我可以在gdb上设置一个断点,但理想情况下我想做的事情如下:
void my_unit_test() {
my_object obj; // perform some initialization that will allocate
START_CHECKING_FUNCTION(malloc); // also, operator new or std::allocate would be nice
obj.perform_realtime_stuff();
STOP_CHECKING_FUNCTION(malloc);
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,std::abort
如果在两个检查之间调用malloc ,那么测试将以不太脏的方式(例如不是)失败.
理想情况下,这可以在任何系统上运行,但我现在可以使用只在Linux上运行的东西.这有可能吗?也许通过LD_PRELOAD hack来取代malloc
,但我不想为我感兴趣的所有函数做这个.
我想制作一个既适用于msvc又适用于gcc的项目文件.
对于情况下,以优化速度,你会做cl /O2
和g++ -O3
.
但我不知道如何告诉项目文件有所作为.我想要像:
msvc:QMAKE_CXXFLAGS_RELEASE += /O2 /openmp /arch:AVX
else:QMAKE_CXXFLAGS_RELEASE += -O3 -march=native -fopenmp -D_GLIBCXX_PARALLEL
Run Code Online (Sandbox Code Playgroud)
哪个不好用.
其他方式是更改mkspecs并使用我的应用程序捆绑新的,但它不是非常便携.谢谢 !
请考虑以下示例代码:
#include <tuple>
void blah();
int buh;
constexpr auto get()
{
return std::get<0>(std::make_tuple(&blah, &buh));
}
int main()
{
get();
}
Run Code Online (Sandbox Code Playgroud)
人们可以预期,由于函数get()
是一个常量表达式,它将返回一个常量.
这不是发生的事情:std::make_tuple
,std::get
实例化并调用:https://godbolt.org/g/PkHrTp
现在,如果我们替换get()
by 的实现
constexpr auto get()
{
constexpr auto x = std::get<0>(std::make_tuple(&blah, &buh));
return x;
}
Run Code Online (Sandbox Code Playgroud)
我们得到了预期的行为:该参数x
的计算是优化掉了,甚至在-O0,并且make_tuple
,get
没有实例化,它可以是相当有用的,以减少二进制膨胀.
是否有一种惯用的方式来强制表单的功能constexpr auto foo()
总是像第二个例子中那样?
现在我会诉诸:
#define constexpr_return(X) do { constexpr auto constexpr_return_x_ = X; return constexpr_return_x_; } while(0)
constexpr_return(std::get<0>(std::make_tuple(&blah, &buh)));
Run Code Online (Sandbox Code Playgroud)
例如,但我不知道这是否是最佳的.
c++ ×8
c++11 ×5
c++14 ×4
c++17 ×2
c ×1
constexpr ×1
dictionary ×1
enable-if ×1
for-loop ×1
g++ ×1
gcc ×1
indentation ×1
lambda ×1
linux ×1
openmp ×1
qmake ×1
qt ×1
sfinae ×1
string-view ×1
templates ×1
unit-testing ×1
visual-c++ ×1