诚然,这是一个开放式/主观问题,但我正在寻找关于如何“组织”相同功能的多个替代实现的不同想法。
我有一组几个函数,每个函数都有特定于平台的实现。具体来说,它们对于特定的 SIMD 类型都有不同的实现:NEON(64 位)、NEON(128 位)、SSE3、AVX2 等(以及一种非 SIMD 实现)。
所有函数都有非 SIMD 实现。并非所有函数都专用于每种 SIMD 类型。
目前,我有一个整体文件,它使用一堆 #ifdef 来实现特定的 SIMD 专业化。当我们仅将少数功能专门用于一种或两种 SIMD 类型时,它就起作用了。现在,它变得笨拙了。
实际上,我需要一些功能类似于虚拟/覆盖的东西。非 SIMD 实现在基类中实现,SIMD 专门化(如果有)将覆盖它们。但我不想要实际的运行时多态性。该代码对性能至关重要,许多函数可以(并且应该)内联。
沿着这些思路的东西可以实现我所需要的(这仍然是#ifdefs的混乱)。
// functions.h
void function1();
void function2();
#ifdef __ARM_NEON
#include "functions_neon64.h"
#elif __SSE3__
#include "functions_sse3.h"
#endif
#include "functions_unoptimized.h"
Run Code Online (Sandbox Code Playgroud)
// functions_neon64.h
#ifndef FUNCTION1_IMPL
#define FUNCTION1_IMPL
void function1() {
// NEON64 implementation
}
#endif
Run Code Online (Sandbox Code Playgroud)
// functions_sse3.h
#ifndef FUNCTION2_IMPL
#define FUNCTION2_IMPL
void function2() {
// SSE3 implementation
}
#endif
Run Code Online (Sandbox Code Playgroud)
// functions_unoptimized.h
#ifndef FUNCTION1_IMPL
#define FUNCTION1_IMPL
void function1() { …Run Code Online (Sandbox Code Playgroud) 我正在尝试调整一些预先存在的代码以利用 std::shared_ptr<...>。它是一个“消息传递系统”,因此基本上下文是:
公共方法:
void writeReport(std::shared_ptr<const ReportBase> pReport) {
/* This is a public method for writing a report. I might do
additional stuff in here but end by forwarding to the
private method. */
writeReport_(pReport);
}
Run Code Online (Sandbox Code Playgroud)
私有方法:
void writeReport_(std::shared_ptr<const ReportBase> pReport) {
if( certain conditions )
processReport_(pReport);
writeBytes_(serialize(pReport));
}
Run Code Online (Sandbox Code Playgroud)
加工方法:
void processReport_(std::shared_ptr<const ReportBase> pReport) {
processReportImpl(pReport);
if( certain other conditions )
reportDeque_.push_back(pReport);
}
Run Code Online (Sandbox Code Playgroud)
例如,在上述伪代码中,processReport_(...) 可能是在某些条件下想要实际存储记录的唯一方法。其他方法仅对所指向的对象的内容感兴趣。因此,如果不是有时需要复制shared_ptr processReport_(...)(即“存储”记录),我只需传递const ReportBase *给所有嵌套函数并避免按值传递的开销(即使用计数增量) 。
因此,我想通过std::shared_ptr<const ReportBase>&(也许&&在适当的情况下),但想阻止一些流氓嵌套方法实际修改指针指向的内容。所以我想我想通过const std::shared_ptr<const …