小编Den*_*ank的帖子

让Visual Studio检查文件校验和而不是时间戳,以确定需要重建哪个文件

目前我正在管理一个巨大的C++ pullrequest,我(git)定期对原始主人进行重新定位,以使其保持最新状态.

但是,Visual Studio似乎在rebase之后重建所有源,因为大多数文件的时间戳已更改,但大多数文件的内容保持不变(CMake用作构建系统).

有没有办法告诉Visual Studio编译器检查文件内容是否保持不变(通过校验和)而不是使用写入文件的最后一个时间戳来阻止它重建我的巨大解决方案?

visual-studio visual-studio-2013 visual-studio-2015

11
推荐指数
1
解决办法
579
查看次数

CMake将字符串解释为变量

我正在寻找一种方法将字符串解释为cmake中的变量名.

鉴于:

set(MY_SECRET_VAR "foo")

# later only the name of the variable is known.
set(THE_NAME "MY_SECRET_VAR")

# Now i'm looking for a way to get the value "foo" from the name
# something like:
set(THE_VALUE "${THE_NAME}")

# THE_VALUE should be "foo"
Run Code Online (Sandbox Code Playgroud)

cmake

10
推荐指数
1
解决办法
4031
查看次数

从io_context中删除工作或使用多个io_context对象

目前,我试图使人们有可能删除工作通过排队post或者dispatchio_context.这项工作由少量的库格组排队,工作可以一次性删除:

boost::asio::io_context context;

auto work = [] {
  // ...
};
boost::asio::post(context, std::move(work));

// ... now I want to remove the work
Run Code Online (Sandbox Code Playgroud)

是否有asio库提供的功能?

目前我正在使用的应用程序是使用io_context::run()从多个线程调用的线程池.

我的想法是,我可以创建io_context由线程池调度的多个s,以便一个io_context代表可以通过删除的组io_context::stop().所有io_contexts都将被保存在一个列表中,然后汇集到一个列表中以用于未完成的事件.

但是我相信汇集或等待很多人io_context可能会导致性能问题.有不同的解决方案吗?

c++ boost asynchronous event-loop boost-asio

8
推荐指数
1
解决办法
593
查看次数

为什么gcc缺乏在本地上下文中优化堆分配?

请考虑以下简化示例:

#include <utility>
#include <memory>

int test_lack()
{
    auto lam = []
    {
        return 10;
    };

    // move lam to the heap
    void* ptr = new decltype(lam)(std::move(lam));

    // retrieve the result of lam
    int res = (*static_cast<decltype(lam)*>(ptr))();

    if (ptr) // important
        delete static_cast<decltype(lam)*>(ptr);

    return res;
}
Run Code Online (Sandbox Code Playgroud)

GCC 5.2 -O3将其编译为:

test_lack():
  sub   rsp, 8
  mov   edi, 1
  call  operator new(unsigned long)
  mov   esi, 1
  mov   rdi, rax
  call  operator delete(void*, unsigned long)
  mov   eax, 10
  add   rsp, 8
  ret
Run Code Online (Sandbox Code Playgroud)

Clang …

c++ optimization gcc

6
推荐指数
0
解决办法
90
查看次数

clang-format:中断函数参数而不是函数限定符(noexcept)

我正在寻找一种使用 clang-format(版本 9.0.0)格式化下面的 C++ 代码的方法,这样超过 80 列限制的函数定义在参数声明而不是 C++ 函数限定符之后被破坏,例如noexcept

void scheduler::stop_mark(service &current, service const &stopped) const
    noexcept {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

上面的代码片段显示了我使用LLVM默认样式格式化的代码,下面的代码是我在正确格式化后想要的代码:

void scheduler::stop_mark(service& current,
                          service const& stopped) const noexcept {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

两个片段之间的区别在于,该行在 之后service& current,而不是 之后被断开noexcept

使用LLVM默认样式时,此行为可重现,但我使用以下选项作为参考:

---
BasedOnStyle: LLVM

AlignAfterOpenBracket: Align
AllowAllArgumentsOnNextLine: 'true'
AllowAllConstructorInitializersOnNextLine: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: Empty
AllowShortLambdasOnASingleLine: Empty
AlwaysBreakTemplateDeclarations: 'Yes'
BinPackArguments: 'true'
BinPackParameters: 'true'
BreakConstructorInitializers: BeforeComma
BreakConstructorInitializersBeforeComma: 'true'
ConstructorInitializerIndentWidth: 2
FixNamespaceComments: 'true' …
Run Code Online (Sandbox Code Playgroud)

c++ llvm clang-format

6
推荐指数
1
解决办法
464
查看次数

使用模板化构造函数时禁用默认复制构造函数和分配构造函数

我正在尝试创建一个可复制的类,具体取决于其模板参数(bool Copyable),否则它只能移动。

myclass(myclass&&)myclass(myclass const&)通过模板参数启用时,它应该可以从类型本身(默认构造函数)构造bool Copyable

它还可以myclass使用其他模板参数进行构造,我当前的实现通过模板化构造函数和赋值运算符涵盖了这一点。

这里使用零规则通过继承的copyable辅助结构生成默认构造函数和赋值运算符,当为 false 时,该结构将禁用复制构造函数和复制赋值运算符bool Copyable

template<bool>
struct copyable { };

template <>
struct copyable<false>
{
    // Disables copy construct & copy assign
    copyable() = default;
    copyable(copyable const&) = delete;
    copyable(copyable&&) = default;
    copyable& operator=(copyable const&) = delete;
    copyable& operator=(copyable&&) = default;
};

template<typename A, typename B, typename C>
struct storage_t
{
    // Implementation depends on A, B and C
};

template<typename A, typename B, …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++14

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

将基类中的继承构造函数与自定义构造函数混合

给出以下示例:

struct tag1 { };
struct tag2 { };

template<typename T>
class Base {
public:
  Base(T /*value*/) { }
  Base(tag1) { }
};

template<>
class Base<void> {
public:
  Base() { }
  Base(tag1) { }
};

template<typename T = void>
class MyClass : public Base<T> {
public:
  using Base<T>::Base;

  MyClass(tag2) : Base<T>(tag1{}) { }
};

int main() {
  {
    MyClass<> defaultConstructible;
    MyClass<> tagConstructible(tag2{});
  }
  {
    MyClass<int> valueConstructible(0);
    MyClass<int> tagConstructible(tag2{});
  }
}
Run Code Online (Sandbox Code Playgroud)

该类MyClass可以使用任何类型进行参数化,当类型T等于时,它应该是默认可构造的void,否则它可以从函数中T编写的类型构造main …

c++ templates c++11

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

在模板参数中通过SFINAE选择构造函数

我正在尝试通过SFINAE选择构造函数如下:

template<typename T>
class MyClass
{
public:
    template<typename C, typename = std::enable_if_t<std::is_class<C>::value>>
    MyClass(C) { }

    template<typename C, typename = std::enable_if_t<std::is_pointer<C>::value>>
    MyClass(C) { }
};
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨以下错误:

错误C2535:'MyClass :: MyClass(C)':已定义或声明的成员函数

甚至没有实例化构造函数.

我制定了一个工作但丑陋的解决方案,由于额外的未使用参数,我不想使用它:

template<typename T>
class MyWorkingClass
{
public:
    template<typename C>
    MyWorkingClass(C, std::enable_if_t<std::is_class<C>::value>* = nullptr) { }

    template<typename C>
    MyWorkingClass(C, std::enable_if_t<std::is_pointer<C>::value>* = nullptr) { }
};
Run Code Online (Sandbox Code Playgroud)

这里给出了一个简短的用法示例:

void* ptr = nullptr;
MyClass<int> mc1(ptr);

std::vector<int> vec;
MyClass<int> mc2(vec);

// Shall raise an error
// MyClass<int> mc2(0);
Run Code Online (Sandbox Code Playgroud)

性状std::is_pointerstd::is_class只是一个例子,原来的特点是更加复杂.

有没有办法通过SFINAE选择构造函数而不向构造函数添加另一个参数(可能非常接近第一个appproach)?

c++ templates sfinae c++14

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

CMake 仅针对特定目标设置 CMAKE_RUNTIME_OUTPUT_DIRECTORY

我的整个项目使用

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
Run Code Online (Sandbox Code Playgroud)

将所有运行时二进制文件放置在该bin目录中。

但是我需要将一些 dll 放在名为“scripts”的子目录中,我尝试使用以下代码为特定目标设置变量:

set_target_properties(my_script_dll PROPERTIES
    CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/scripts"
)
Run Code Online (Sandbox Code Playgroud)

但它不起作用,dll仍然放在bin.

有没有办法仅为特定目标设置输出目录?

cmake

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

将编译时已知函数参数转换为std :: integral_constant的有效方法

昨天我读了一篇 关于将编译时已知函数参数从constexpr函数转换为类似类型的博客文章std::integral_constant<>.

可能的用法示例是从用户定义的文字转换类型.

请考虑以下示例:

constexpr auto convert(int i)
{
    return std::integral_constant<int, i>{};
}

void test()
{
    // should be std::integral_constant<int, 22>
    using type = decltype(convert(22));
}
Run Code Online (Sandbox Code Playgroud)

但显然和正如预期的那样,Clang会抛出以下错误:

error: ‘i’ is not a constant expression return std::integral_constant<int, i>{}; ^

上述博客的作者建议使用模板化的用户定义文字将数字拆分为std :: integer_sequence以将其解析为int.

但这个建议对我来说似乎无法使用.

有没有一种有效的方法将编译时已知的函数参数转换为类似的类型std::integral_constant<>

c++ templates c++14

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