将浮点常量声明为static constexpr
变量和函数(如下例所示)之间是否存在差异,还是仅仅是样式问题?
class MY_PI
{
public:
static constexpr float MY_PI_VAR = 3.14f;
static constexpr float MY_PI_FUN() { return 3.14f; }
}
Run Code Online (Sandbox Code Playgroud) 是否可以检查该类型T
是否std::array
为任意类型和大小?
我可以检查一个特定的数组,例如:
is_same<T, std::array<int,5>>::value
Run Code Online (Sandbox Code Playgroud)
但我想检查一下T
是否有任何实例化std::array
.像下面的东西(当然,这不会编译):
is_same<T, std::array>::value
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这一点(可能没有使用is_same
)?
我正在研究提升线程.我发现线程类有一个接受可调用对象的构造函数.什么是可调用对象?
class CallableClass
{
private:
// Number of iterations
int m_iterations;
public:
// Default constructor
CallableClass()
{
m_iterations=10;
}
// Constructor with number of iterations
CallableClass(int iterations)
{
m_iterations=iterations;
}
// Copy constructor
CallableClass(const CallableClass& source)
{
m_iterations=source.m_iterations;
}
// Destructor
~CallableClass()
{
}
// Assignment operator
CallableClass& operator = (const CallableClass& source)
{
m_iterations=source.m_iterations;
return *this;
}
// Static function called by thread
static void StaticFunction()
{
for (int i=0; i < 10; i++) // Hard-coded upper limit
{ …
Run Code Online (Sandbox Code Playgroud) gcc(最新版本:4.8,4.9)是否具有类似于__assume()
icc支持的内置的"假设"子句?例如,__assume( n % 8 == 0 );
看起来我无法将无捕获的lambda作为模板参数传递给模板化的函数指针函数.我这样做是错误的,还是不可能的?
#include <iostream>
// Function templated by function pointer
template< void(*F)(int) >
void fun( int i )
{
F(i);
}
void f1( int i )
{
std::cout << i << std::endl;
}
int main()
{
void(*f2)( int ) = []( int i ) { std::cout << i << std::endl; };
fun<f1>( 42 ); // THIS WORKS
f2( 42 ); // THIS WORKS
fun<f2>( 42 ); // THIS DOES NOT WORK (COMPILE-TIME ERROR) !!!
return 0;
}
Run Code Online (Sandbox Code Playgroud) 关于以下shared_ptr
构造函数的问题:
template< class Y >
shared_ptr( const shared_ptr<Y>& r, T *ptr );
Run Code Online (Sandbox Code Playgroud)
我是否更正,如果r
是使用用户提供的删除器创建的,那么别名就shared_ptr
知道了.因此,如果别名shared_ptr
是组中的最后一个,并且(当超出范围时)破坏最初由其管理的资源r
,它将使用该用户提供的删除器吗?
是否可以static const
使用模板参数包中的值创建数组?我尝试了以下代码,但gcc 4.8.1给出了"错误:参数包未展开"
template<int... N>
struct ARRAY_OF_DIMS
{
static constexpr size_t NDIM = sizeof...(N);
static const int DIMS[NDIM];
};
template<int... N>
const int ARRAY_OF_DIMS<N>::DIMS[ARRAY_OF_DIMS<N>::NDIM] = { N... };
Run Code Online (Sandbox Code Playgroud) 我有两个数组:char* c
并且float* f
我需要执行此操作:
// Compute float mask
float* f;
char* c;
char c_thresh;
int n;
for ( int i = 0; i < n; ++i )
{
if ( c[i] < c_thresh ) f[i] = 0.0f;
else f[i] = 1.0f;
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种快速的方法:没有条件,如果可能的话使用SSE(4.2或AVX).
如果使用float
而不是char
可以导致更快的代码,我可以更改我的代码只使用浮点数:
// Compute float mask
float* f;
float* c;
float c_thresh;
int n;
for ( int i = 0; i < n; ++i )
{
if ( c[i] < c_thresh …
Run Code Online (Sandbox Code Playgroud) 或者它可以是一个单独的无符号整数类型?
我对不同的(无符号)整数类型有不同的模板函数特化.我需要提供单独的专业size_t
吗?
我使用8个OpenMP线程运行以下循环:
float* data;
int n;
#pragma omp parallel for schedule(dynamic, 1) default(none) shared(data, n)
for ( int i = 0; i < n; ++i )
{
DO SOMETHING WITH data[i]
}
Run Code Online (Sandbox Code Playgroud)
由于NUMA,我想用线程0,1,2,3和后半部分(i = n/2,...)运行循环的前半部分(i = 0,...,n/2-1). ..,n-1)与线程4,5,6,7.
本质上,我想并行运行两个循环,每个循环使用一组独立的OpenMP线程.
如何使用OpenMP实现这一目标?
谢谢
PS:理想情况下,如果来自一个组的线程完成了它们的一半循环,而另一半循环仍然没有完成,我希望来自完成组的线程加入未完成的组处理循环的另一半.
我正在考虑下面的内容,但我想知道我是否可以使用OpenMP执行此操作并且不需要额外的簿记:
int n;
int i0 = 0;
int i1 = n / 2;
#pragma omp parallel for schedule(dynamic, 1) default(none) shared(data,n,i0,i1)
for ( int i = 0; i < n; ++i )
{
int nt = omp_get_thread_num();
int j; …
Run Code Online (Sandbox Code Playgroud)