我需要一种快速的方法来获得float具有给定位模式(提供为int32_t).当然,编译器应该优化整个构造.简单的转换会进行转换,reinterpret_cast<>不允许...
我有一个类,它包含一些静态大小的容器:
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编译.
如果我有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) 假设,我有一个使用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()创建容器的副本而不是通过引用传递原始容器.有没有办法强制执行参考?
我正在使用emacs 23.1.1(在linux上)来编辑我的软件.我通常使用短切alt-shft-5进行更换.但是,如果我意外地击中了alt-shft-4,emacs冻结说"检查CC的拼写......"
如何中断/停止此操作以返回编辑会话?
我有一个函数接受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的内容应该在它退出函数时更新,对吧?
我读了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) 检查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 头文件而导致的命名空间污染问题。例如,在我的 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()与定义不匹配 …
我正在努力让这项工作正常进行,但似乎有一些我不完全理解的事情。
\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\nRun 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}*/\nRun Code Online (Sandbox Code Playgroud)\n\n在我的主要内容中,我有这个来测试互斥体:
\n\nCommonSingleton::getInstance().GetMutexWB.lock();\nRun Code Online (Sandbox Code Playgroud)\n\n但现在我评论了,只是为了弄清楚如何编译。这是我得到的错误: …