我正在尝试设计一个应用安全bool习语的bool包装器结构.
解决这个问题的经典实现非常简单:骨架可能是这样的:
struct Bool final
{
Bool() = default;
Bool(bool value)
: _value{value}
{}
explicit operator bool() const {
return _value;
}
private:
bool _value{false};
};
Run Code Online (Sandbox Code Playgroud)
我试图改进的部分是如何Bool
构建的.
例如,我想通过设计避免隐式缩小:
Bool b1(45); // yields warnings, but it compiles
Bool b2{3}; // not ok by standard
Run Code Online (Sandbox Code Playgroud)
我试图用模板伤害自己,但没有成功.
我怎么能让它工作?
clang
支持插件,这个概念通常用于构建静态分析等工具。为了开始使用它,我举了这个例子,它打印目标 cpp 文件中存在的所有函数名称。
我编译了运行以下命令的插件:
clang++ -v -std=c++11 PrintFunctionNames.cpp \
$(llvm-config --cxxflags --ldflags) \
-o plugin.so -shared -Wl,-undefined,dynamic_lookup
Run Code Online (Sandbox Code Playgroud)
然后“按书本”运行:
clang++ \
-c main.cpp \
-Xclang -load \
-Xclang $PWD/plugin.so \
-Xclang -plugin \
-Xclang print-fns
Run Code Online (Sandbox Code Playgroud)
它工作得很好:它打印 main.cpp 中的函数名称
并退出(由于 -c 标志而无需编译 main.cpp)。
What I'd like to do is to print all the function names AND compile main.cpp into an executable.
I tried removing the -c
flag but I got:
/usr/bin/ld: cannot find /tmp/main-284664.o: No such file or directory …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个简单的仅演示标头的名为foo的库,并要求它另一个名为bar的库。
foo的结构是这样的:
foo / include / foo.hpp-只是一个测试头文件...
foo / conanfile.py
from conans import ConanFile, CMake
class FooConan(ConanFile):
name = "foo"
version = "0.0.1"
exports = "*"
Run Code Online (Sandbox Code Playgroud)
使用以下命令将其导出:
conan export steazzalini/testing
Run Code Online (Sandbox Code Playgroud)
bar / conanfile.txt
[requires]
foo/0.0.1@steazzalini/testing
[generators]
cmake
Run Code Online (Sandbox Code Playgroud)
柯南安装失败说:
ERROR: Can't find a 'foo/0.0.1@steazzalini/testing' package for the specified options and settings
- Try to build from sources with "--build foo" parameter
- If it fails, you could try to …
Run Code Online (Sandbox Code Playgroud) 我正在使用python项目unittest
进行测试以及coverage
代码覆盖.
我广泛使用接口模式,我注意到整体代码覆盖率很大程度上受"未经测试的接口"的影响.
考虑以下:
class IReader(object):
@abstractmethod
def read(self):
pass
class Reader(IReader):
def read(self):
# whatever
Run Code Online (Sandbox Code Playgroud)
我测试Reader
但(显然)我没有测试,IReader
因此pass
指令被标记为未被测试覆盖.
有没有办法忽略接口coverage
?
由于这是我的第一个python项目之一,我这样做完全错了吗?
我开始尝试constexpr
.
我想要实现的是验证literal
作为ctor参数提供的数值.
我开始使用以下内容,如果构造MyStruct
值<= 4 则抛出.
constexpr int validate(int v)
{
return (v > 4) ? v : throw exception();
};
struct MyStruct final
{
constexpr MyStruct(const int v)
: _v{validate(v)}
{
}
void add(int toAdd)
{
_v += toAdd;
}
int _v;
};
int main(int argc, char**)
{
constexpr MyStruct a{500}; // ok so far...
a.add(argc); // ...nope
MyStruct b{500}; // check at runtime :(
MyStruct c{argc}; // runtime check ok
}
Run Code Online (Sandbox Code Playgroud)
标记MyStruct …
我正在设计一个基于virtual
方法的C++类接口,以便能够提供可扩展性点.
许多这些公共方法需要heap
分配对象作为参数.
由于我正在使用现代C++模式,我打算使用std::unique_ptr
或std::shared_ptr
为此,但我对它们都有疑问.
使用std :: unique_ptr它看起来像这样:
class IFoo {
virtual void doSomethingWithUser(std::unique_ptr<User> user) = 0;
}
Run Code Online (Sandbox Code Playgroud)
强制调用者提供的std::unique_ptr
缺点有:
doSomethingWithUser
实现需要将用户存储在某个容器中,则它是不可构造的std::shared_ptr
使用std::shared_ptr
所有公共方法可以解决问题,但我们必须支付额外的内存空间加上引用计数的原子增量和减量.
我可以遵循任何经验法则吗?
c++ ×5
clang ×2
llvm ×2
boolean ×1
c++17 ×1
conan ×1
constexpr ×1
heap ×1
if-constexpr ×1
interface ×1
lld ×1
musl ×1
plugins ×1
python ×1
shared-ptr ×1
toolchain ×1
unique-ptr ×1
unit-testing ×1
validation ×1