小编Wal*_*ter的帖子

如何在C++中使用给定的位模式(如int32_t)获取浮点数?

我需要一种快速的方法来获得float具有给定位模式(提供为int32_t).当然,编译器应该优化整个构造.简单的转换会进行转换,reinterpret_cast<>不允许...

c++ type-conversion c++11

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

根据成员容器的大小专门化成员函数

我有一个类,它包含一些静态大小的容器:

template <typename Container>
struct Point {
    Container container;
    ... 
    void bar();
}
Run Code Online (Sandbox Code Playgroud)

其中一Container类可能是这样的:

struct Container1 {
    static constexpr size_t size = 5;
}
Run Code Online (Sandbox Code Playgroud)

现在我想bar根据容器的大小来专门化方法.我不明白该怎么做.

编辑:

我想要一个C++ 11解决方案.C++ 14可能有用,但我们使用的编译器通常具有不稳定的C++ 14支持.

编辑:

Stack Danny提出了一个解决方案,它与Clang编译,但不与GCC编译.

c++ templates template-specialization c++11

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

8个32位浮点数的水平总和

如果我有8个打包的32位浮点数(__m256),那么提取所有8个元素的水平和的最快方法是什么?同样,如何获得水平最大值和最小值?换句话说,以下C++函数的最佳实现是什么?

float sum(__m256 x);  ///< returns sum of all 8 elements
float max(__m256 x);  ///< returns the maximum of all 8 elements
float min(__m256 x);  ///< returns the minimum of all 8 elements
Run Code Online (Sandbox Code Playgroud)

x86 sse simd avx

3
推荐指数
2
解决办法
3020
查看次数

是否可以将引用传递给可变参数模板函数?

假设,我有一个使用CRTP的基类并提供一个可变参数模板静态成员函数

template<typename derived_task>
struct task_impl : library::task
{
   /* some useful functionality implemented using CRTP */
   /// static method for spawning a task.
   template<typename... Args>
   static void spawn(Args... args)
   { library::spawn(new task(args...)); }
 };
Run Code Online (Sandbox Code Playgroud)

和派生类

struct my_task : task_impl<my_task>
{
  /* implementation using functionality of task_impl<> */
  my_task(container&c, int i);
};
Run Code Online (Sandbox Code Playgroud)

然后想要使用variadic模板成员via

container c( /* args for ctor */ );
my_task::spawn(c,0);
Run Code Online (Sandbox Code Playgroud)

这里发生的是spawn()创建容器的副本而不是通过引用传递原始容器.有没有办法强制执行参考?

c++ pass-by-reference variadic-templates c++11

3
推荐指数
1
解决办法
1789
查看次数

如何中断emacs拼写检查(冻结)

我正在使用emacs 23.1.1(在linux上)来编辑我的软件.我通常使用短切alt-shft-5进行更换.但是,如果我意外地击中了alt-shft-4,emacs冻结说"检查CC的拼写......"

如何中断/停止此操作以返回编辑会话?

emacs

3
推荐指数
1
解决办法
457
查看次数

将引用传递给指向void类型的指针

我有一个函数接受void**的引用.

bool boExecute(void**& vOutParameter);
Run Code Online (Sandbox Code Playgroud)

我试图在vOutParameter中写一些值,但是当我在main()中检查它时,没有写入值.

在这种情况下,什么和引用?它是对指针的引用还是对指针指针的引用?

在boExecute中,我这样添加:

bool boExecute(void**& vOutParameter)
{
    Struct_Type* Out = new Struct_Type[4];
    for (int i=0; i<4; ++i)
    {
        memcpy(&(Out[i]), Referenced_Struct[i], sizeof(Struct_Type));
    }
    *vOutParameter = reinterpret_cast<void*>Out;
    Out = null;
    return true;
}
Run Code Online (Sandbox Code Playgroud)

Referenced_Struct的类型为Struct_Type**,它有两个成员int32_val和int64_val.

主要内容:

void main(void)
{
   void **test;
   boExecute(test);
   Struct_Type** temp = reinterpret_cast<Struct_Type**>(test);
   Struct_Type* temp1 = *temp;
   for (int i=0; i<4; ++i)
   {
       printf("%d, %d", temp1[i].int32_val, temp1[i].int64_val);
   }
}
Run Code Online (Sandbox Code Playgroud)

我正在做的事情有什么不对吗?当我更改*vOutParameter时,*vOutParameter的内容应该在它退出函数时更新,对吧?

c++ pointers pass-by-reference

3
推荐指数
1
解决办法
79
查看次数

为什么operator->在C++ STL库中由operator*实现?

我读了std::deque源代码,我找到了以下的实现_deque_iterator::operator->_deque_iterator::operator*

reference operator* const() { return *cur; }
pointer operator-> const(){ return &(operator*()); }
Run Code Online (Sandbox Code Playgroud)

所以我的问题是为什么不只是返回cur指针?像这样:

pointer operator-> const(){ return cur; }
Run Code Online (Sandbox Code Playgroud)

c++ stl

3
推荐指数
1
解决办法
129
查看次数

如何检查AVX内在__m256的inf

检查AVX内在函数__m256(8的向量float)是否包含任何内容的最佳方法是什么inf?我试过了

__m256 X=_mm256_set1_ps(1.0f/0.0f);
_mm256_cmp_ps(X,X,_CMP_EQ_OQ);
Run Code Online (Sandbox Code Playgroud)

但相比之下true.请注意,此方法将找到nan(与之比较false).所以一种方法是检查X!=nan && 0*X==nan:

__m256 Y=_mm256_mul_ps(X,_mm256_setzero_ps());   // 0*X=nan if X=inf
_mm256_andnot_ps(_mm256_cmp_ps(Y,Y,_CMP_EQ_OQ),
                 _mm256_cmp_ps(X,X,_CMP_EQ_OQ));
Run Code Online (Sandbox Code Playgroud)

但是,这看起来有点冗长.有更快的方法吗?

c c++ sse intrinsics avx

3
推荐指数
1
解决办法
1437
查看次数

标准头文件对全局命名空间的污染

我一次又一次地遇到由于从 C++ 标准头文件间接包含 C 头文件而导致的命名空间污染问题。例如,在我的 Linux 系统上,gcc(版本 5.1.1)<thread>包含usr/include/bits/sched.h,它声明

extern "C" {
  extern int clone(int (*__fn) (void *__arg), void *__child_stack, int __flags, void *__arg, ...) throw();
}
Run Code Online (Sandbox Code Playgroud)

在下面的最小示例中

#include <thread>                              // indirect inclusion of <sched.h>

namespace {
  struct foo
  { virtual foo*clone() const=0; };

  foo*clone(std::unique_ptr<foo> const&);      // function intended

  struct bar : foo
  {
    std::unique_ptr<foo> daughter;
    bar(foo*d) : daughter(d) {}
    foo*clone() const
    { return new bar(::clone(daughter)); }     // to be called here
  };
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨调用::clone()与定义不匹配 …

c++ namespaces standards-compliance language-lawyer

3
推荐指数
1
解决办法
1159
查看次数

C++ 中互斥体的单例类

我正在努力让这项工作正常进行,但似乎有一些我不完全理解的事情。

\n\n

我在一个文件中有一个多线程应用程序(2个类和我在其中创建线程和所有内容的真正的主程序)。我想分成3个文件。一个用于每个类(及其标题),一个用于主类。

\n\n

我有几个 mutices 和一个队列来在线程之间共享消息并随时锁定和解锁它们。

\n\n

我想编写一个单例类,以便所有类都可以共享互斥锁:

\n\n

标头:

\n\n
#ifndef COMMONSINGLETON_H\n#define COMMONSINGLETON_H\n\n#include <mutex>          // std::mutex\n#include <queue>\n\nclass CommonSingleton{\n    private:\n        static std::mutex               mutex_w_b;\n        //static std::mutex               mutex_wifi_ok;\n        //static std::queue <std::string> queue_w_b;\n\n    public:\n        static CommonSingleton& getInstance();\n        std::mutex GetMutexWB();\n        //std::mutex GetMutexWiFiOk();\n        //std::queue <std::string> GetQueueWB();\n    private:\n        CommonSingleton() {};\n        CommonSingleton(CommonSingleton const&);\n        void operator=(CommonSingleton const&);\n};\n#endif\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的 .cpp 文件:

\n\n
#include "commonsingleton.h"\n\nstatic CommonSingleton& CommonSingleton::getInstance(){\n    static CommonSingleton instance;\n    return instance;\n}\nstd::mutex CommonSingleton::GetMutexWB(){\n    return mutex_w_b;\n}\n/*std::mutex CommonSingleton::GetMutexWiFiOk(){\n    return mutex_wifi_ok;\n}\nstd::queue <std::string> CommonSingleton::GetQueueWB(){\n   return queue_w_b;\n}*/\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我的主要内容中,我有这个来测试互斥体:

\n\n
CommonSingleton::getInstance().GetMutexWB.lock();\n
Run Code Online (Sandbox Code Playgroud)\n\n

但现在我评论了,只是为了弄清楚如何编译。这是我得到的错误: …

c++ multithreading mutex c++11

3
推荐指数
1
解决办法
3393
查看次数