小编Lor*_*one的帖子

rvalue-reference to array:它真的会发生吗?

考虑以下代码:

#include <iostream>
using namespace std;

typedef int array[12];

array sample;

array ret1(){   //won't compile
    return sample;
}

array& ret2(){
    return sample;
}

array&& ret3(){
    return sample;  //won't compile
}

void eat(array&& v){
    cout<<"got it!"<<endl;
}

int main(){
    eat(ret1());
    eat(ret2());    //won't compile
    eat(ret3());    //compiles, but I don't really know how to write a function that returns a rvalue-reference to an array
}
Run Code Online (Sandbox Code Playgroud)

实际上似乎编译的唯一版本是ret3().实际上,如果我省略了实现并且只是声明它,它会编译(当然也不会链接),但实际上我不知道如何显式地将rvalue引用返回给数组.如果这不可能发生,那么我可以得出结论,对数组的rvalue-reference不是禁止的,但是不能使用吗?

编辑:

我才意识到这有效:

array&& ret3(){
    return std::move(sample);
}
Run Code Online (Sandbox Code Playgroud)

现在有趣的是了解它的实际价值......

c++ arrays rvalue-reference move-semantics c++11

5
推荐指数
1
解决办法
1060
查看次数

为什么我不能将指向Derived类成员函数的指针强制转换为类Base?

对我来说,将a转换void(Derived::*)()为a 看起来非常安全void(Base::*)(),就像在这段代码中一样:

#include <iostream>
#include <typeinfo>
using namespace std;
struct Base{
    void(Base::*any_method)();
    void call_it(){
        (this->*any_method)();
    }
};
struct Derived: public Base{
    void a_method(){
        cout<<"method!"<<endl;
    }
};
int main(){
    Base& a=*new Derived;
    a.any_method=&Derived::a_method;
    a.call_it();
}
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨演员a.any_method=&Derived::a_method;.这是一个阻止细微编程错误的障碍,还是一些让编译器编写者生活更轻松的东西?是否有变通方法让Base类具有指向Derived 无类型知识的成员函数的指针(也就是说,我不能Base使用模板参数创建模板Derived).

c++ member-function-pointers

5
推荐指数
1
解决办法
1879
查看次数

在unordered_set中插入一个新元素:提示应该是end()吗?

如果我确定某个值还没有进入unordered_set,并且我要插入这样的值,那么将此set end()迭代器作为提示传递是否正确?

编辑:

码:

#include <unordered_set>
using namespace std;

unordered_set<int> someset;

int main(){
    auto it=someset.find(0);
    if(it==someset.end()) someset.insert(it, 0);    //correct? possible performance boost if the set is actually populated?
}
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11

5
推荐指数
1
解决办法
6522
查看次数

必须重新编译什么才能运行x32 ABI应用程序?

我可以使用新的x32 ABI编译应用程序,然后在普通内核中运行它吗?那么运行时C库呢?与预编译的x86 / x86_64库是否存在任何形式的互操作性?

linux abi 32bit-64bit linux-x32-abi

5
推荐指数
1
解决办法
471
查看次数

long double(80位)是使用-funsafe-math-optimizations的两倍

我一直认为使用long double数据类型的速度大约是使用double时计算速度的两倍-funsafe-math-optimizations.我想对此有所了解,因为80位格式已经弃用了很长时间,或者我可能正在做一些非常愚蠢的double数据类型.编译器是g ++ 4.8.2,目标是x86_64(所以如果我不使用,gcc会更喜欢SSE2 long double).

我的代码或多或少像这样(伪代码):

//x is an array of floating point numbers
for i -> x.size
        accumulator = 0
        for k -> kmax
            accumulator += A[k]*(B[k]*cos(C*k*x[i]) - D[k]*sin(C*k*x[i]));
        x[i] += F*accumulator;
        if(x[i] >= 1/2) x[i] -= integer(x[i]+1/2);
        else if(x[i] < -1/2) x[i] -= integer(x[i]-1/2);
Run Code Online (Sandbox Code Playgroud)

A,, B...是一些预先计算的数组/常量.

加速似乎与高速缓存行问题无关,因为如果我使用OpenMP并行化外部for循环,我会获得相同的相对加速.

编辑:我纠正了伪代码:注意cos并且sin有相同的参数,这最终是加速的原因(参见gsg的答案和评论).

c floating-point gcc compiler-optimization

5
推荐指数
0
解决办法
369
查看次数

Java数组:synchronized + Atomic*,还是同步的?

这个问题一再被问到,但我仍有疑问.当人们说同步创建了一个内存屏障时,这个内存屏障适用于什么,任何缓存变量?这看起来不太可行.

所以,由于这个疑问,我写了一些看起来像这样的代码:

final AtomicReferenceArray<Double> total=new AtomicReferenceArray<Double>(func.outDim);
for(int i=0; i<func.outDim; i++) total.set(i, 0.);
for(int i=0; i<threads; i++){
    workers[i]=new Thread(new Runnable(){
        public void run() {
            double[] myPartialSum=new double(func.outDim);
            //some lengthy math which fills myPartialSum...

            //The Atomic* guarantees that I'm not writing local copies of the Double references (whose value are immutables, so it's like an array of truly volatile doubles) in variable total, synchronized(total) atomizes the sum
            synchronized(total){ for(int i=0; i<func.outDim; i++) total.set(i, total.get(i)+myPartialSum[i]); }
        };
    workers[i].start();
}
//wait for workers to …
Run Code Online (Sandbox Code Playgroud)

java arrays atomic volatile synchronized

4
推荐指数
1
解决办法
331
查看次数

静态字段模板特化的"未定义引用"

我有一个模板类头文件,它只有静态函数和字段.

template<typename T> class Luaproxy {

    static std::map<std::string, fieldproxy> fields;
    static const char * const CLASS_NAME;
    static void addfields();
    static int __newindex(lua_State * l){
        //implemented stuff, references to fields...
    }
    //etc
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,只会声明一些函数,因为我打算用模板特化来实现它们.

在.ccp文件中我有:

struct test { int a; }
template<> map<string, fieldproxy> Luaproxy<test>::fields;
template<> const char * const Luaproxy<test>::CLASS_NAME=typeid(test).name();
template<> void Luaproxy<test>::addfields(){
    //stuff, references to fields...
}
Run Code Online (Sandbox Code Playgroud)

Luaproxy<test>::fields从头文件中实现的函数和仅专门用于.cpp的函数中获取未定义的引用错误.请注意,Luaproxy<test>::CLASS_NAMELuaproxy<test>::addfields似乎在联系被发现.

是什么让map这么特别?

c++ linker static templates

4
推荐指数
1
解决办法
2113
查看次数

c ++ 11中的typeid(T).name()替代?

在c ++ 11中是否有一种标准方法可以使用某些模板黑魔法或使用某些标准库函数动态获取类的名称?

c++ reflection c++11

4
推荐指数
2
解决办法
4418
查看次数

result_of对我不起作用

#include <type_traits>
using namespace std;

struct asd{
    void f();
};

int f();

typedef typename result_of<decltype(f)>::type result_free;
typedef typename result_of<decltype(&asd::f)>::type result_mem;
Run Code Online (Sandbox Code Playgroud)

两个typedef都会出错

In file included from ../main.cpp:1:0:
/usr/include/c++/4.6/type_traits: In instantiation of ‘std::_Result_of_impl<false, false, int>’:
/usr/include/c++/4.6/type_traits:1215:12:   instantiated from ‘std::result_of<int()>’
../main.cpp:10:41:   instantiated from here
/usr/include/c++/4.6/type_traits:1192:9: error: ‘std::declval [with _Tp = int, typename std::add_rvalue_reference<_Tp>::type = int&&]()’ cannot be used as a function
../main.cpp:10:43: error: invalid combination of multiple type-specifiers
../main.cpp:10:59: error: invalid type in declaration before ‘;’ token
../main.cpp:11:49: error: ‘type’ in ‘struct std::result_of<void …
Run Code Online (Sandbox Code Playgroud)

c++ result-of sfinae c++11

4
推荐指数
1
解决办法
1680
查看次数

对 pthread 互斥体涉及的完整内存屏障的澄清

我听说在处理互斥锁时,必要的内存屏障是由 pthread API 本身处理的。我想了解有关此事的更多细节。

  1. 这些说法是真的吗,至少在最常见的架构上是这样吗?
  2. 编译器是否能够识别这种隐式屏障,并在生成代码时避免对操作/从本地寄存器读取进行重新排序?
  3. 何时应用内存屏障:成功获取互斥体之后和释放它之后?

synchronization mutex pthreads memory-fences

4
推荐指数
1
解决办法
1319
查看次数