小编Gui*_*cot的帖子

默认参数,如果默认可构造

我有一个小问题,我的代码有效,但它并不理想.

我有一个函数,它以谓词作为参数.我想支持这些语法:

myClass.foo([](auto param){ return true; }); // predicate sent as parameter
myClass.foo<PredicateType>(); // predicate type sent as template argument
myClass.foo(); // default predicate took
Run Code Online (Sandbox Code Playgroud)

但是,为了做到这一点,我重复了这样的功能:

struct MyClass {
    template<typename Predicate = DefaultPredicate&&, std::enable_if_t<std::is_default_constructible<detail::decay_t<Predicate>>::value, int> = 0>
    void foo(Predicate&& p = std::decay_t<Predicate>{}) {
        // stuff...
    }

    template<typename Predicate, std::enable_if_t<!std::is_default_constructible<detail::decay_t<Predicate>>::value, int> = 0>
    void foo(Predicate&& p) {
        // exact same stuff...
    }
};
Run Code Online (Sandbox Code Playgroud)

有没有办法在不重复我的代码的情况下执行此操作?理想的是拥有一个功能.我怎么能这样做,最好是在c ++ 11?

c++ templates sfinae c++11

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

获取每种参数包的大小?

经过漫长的一天不成功的网上搜索找到解决问题的实用解决方案后,我决定在这里发布我的问题,以澄清我的目标,我提供了这个简单的代码:

template<typename... types>
std::vector<SIZE_T> GetTypesSize()
{
    std::vector<SIZE_T> typesSizeContainer;
    typesSizeContainer.reserve(sizeof... (types)); 

    /*
     * What I want here is a mechanism to loop throw 
     * each element of the parameter pack to get its size 
     * then push it into typesSizeContainer.   
     * Something similar to :
     *
     *     for(auto& element : types...) {
     *         typesSizeContainer.push(sizeof(element));
     *     }
     * 
     */

    return std::move(typesSizeContainer);
}
Run Code Online (Sandbox Code Playgroud)

当我在这段代码中调用此函数模板时:

// platform x86
std::vector<SIZE_T> tempVactor;
tempVactor = GetTypesSize<char, short, int>(); 
Run Code Online (Sandbox Code Playgroud)

tempVactor应该是的元素{ 1, 2, 4 }.

任何建议或解决方案都相当可观.

c++ templates variadic

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

单元测试高度模板化的库

我想知道单元测试模板是否是一件事。让我解释一下我的需求。

我有一个高度模板化的库。我有很多 sfinae 类型特征,还有一些 static_assert。

我想测试的是 sfinae 类型特征的有效性,并测试我的 static_assert 是否抛出正确的东西。知道我的报道内容会很棒。

这是我的代码的示例:

template<typename T>
using false_v = !std::is_same<T, T>::value;

// Here are my types traits
template<typename T, typename... Args>
struct SomeCondition1 { /* ... */ };

template<typename T, typename... Args>
struct SomeCondition2 { /* ... */ };

// This is a master type trait, that test every others
template<typename T, typename... Args>
using Conditions = std::integral_constant<bool,
    SomeCondition1<T, Args...>::value && SomeCondition2<T, Args...>::value
>;

// This is the function that is call when …
Run Code Online (Sandbox Code Playgroud)

c++ unit-testing template-meta-programming

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

如何使用 std::copy 将 constexpr 数组复制到另一个 constexpr 数组?

在下面的代码中,我创建了一个长度为 6 的数组,并在前 3 个元素中使用 1、2 和 3 对其进行初始化。然后我将前 3 个元素复制到后 3 个元素。然后我按顺序打印所有元素。

\n\n
std::array<int, 6> bar = {1, 2, 3};\n\nint main(){\n    // Copy the first 3 elements to the last 3 elements\n    std::copy(bar.begin(), bar.end() - 3, bar.end() - 3);\n\n    // Print all the elements of bar\n    for(auto& i: bar) std::cout << i << std::endl;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

它工作正常,但是当我尝试创建数组时,constexpr它不再编译:

\n\n
constexpr std::array<int, 6> bar = {1, 2, 3};\n\nint main(){\n    // Copy the first 3 elements to the last 3 elements\n …
Run Code Online (Sandbox Code Playgroud)

c++ arrays algorithm constexpr c++11

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

是否可以使用noreturn noexcept函数而不是调用std :: terminate?

我正在制作一个小错误处理系统,我想做一个终止程序的致命错误.我想到了两种方法:

[[noreturn]] inline void fatal_error1(char const* msg) {
    std::terminate();
}

[[noreturn]] inline void fatal_error2(char const* msg) noexcept {
    throw std::runtime_error{msg};
}
Run Code Online (Sandbox Code Playgroud)

是否有理由fatal_error2不建议使用?该函数的目标是终止程序,我甚至将其标记为noreturn,但每个人似乎都告诉我不要抛出noexcept函数.

我很想使用fatal_error2它,因为它what()在终端输出,而我需要在该标题中包含一些输出函数来打印消息fatal_error1.

c++ noexcept c++14

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

单独程序的多对一双向通讯

我正在尝试在Linux中的程序之间进行双向多对一通信。

我的计划如下:一个与硬件对话的名为“驱动程序”的程序需要与Linux中未知数量的应用程序进行通信。我读到,用于进程间通信的最常见方法之一是“命名管道”。

我还没有发现的问题是:新程序应如何通知驱动程序新程序正在运行,以便在新程序与驱动程序之间建立另一个连接(命名管道)?

所有程序将用C ++编写

在此处输入图片说明

c++ linux named-pipes

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

由于不一致导致模板参数推导失败

请考虑以下内容(它不会编译,但稍后会修复):

void foo(const int *n) { }

template <typename ...Args>
void bar(void (*func)(Args...), Args... args) { func(args...); }

int main(int argc, char *argv[])
{
    int n = 42;
    bar(foo, &n);
}
Run Code Online (Sandbox Code Playgroud)

模板函数bar()需要一个函数指针来调用,并将参数包传递给它。gcc 7.4.0诊断以下错误:

test.cpp:6:6: note:   template argument deduction/substitution failed:
test.cpp:11:16: note:   inconsistent parameter pack deduction with ‘const int*’ and ‘int*’
Run Code Online (Sandbox Code Playgroud)

很明显,类型推导规则不够宽松,无法const T*同时观察到const T*和被推导T*。好的。这很容易用演员表修复

bar(foo, static_cast<const int *>(&n));
Run Code Online (Sandbox Code Playgroud)

但这是丑陋的。C ++ 17具有std::as_const()使它变得不那么丑陋的(&std::as_const(n))的功能,但是在我当前的项目中,我仅限于C ++ 14的sadface。 …

c++ const template-argument-deduction c++14

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

Keep Class Type after calling `operator[]` in nested Class

The below class is meant to be a top-layer class which brings all the benefits of nlohman::json but offers additional functions.

#include <nlohmann/json.hpp>

class Other { /* ... */ };

class AbstractData : public nlohmann::json
{
public:
    AbstractData (const nlohmann::json& json) : nlohmann::json(json) { }

    Other createOther(const char* key) { /* create Other class using key */ }
    std::string toString() { /* convert to string */ }
    /* etc. */
};
Run Code Online (Sandbox Code Playgroud)


But I ran into issues when using operator[]. …

c++ inheritance class function c++17

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

为什么python需要global关键字而C/C++不需要?

如果我想更改全局变量,我可以直接在 C++ 中执行:

#include <stdio.h>

int x = 1;

int main()
{
    x = 1 + x;
    printf("%d\n", x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但使用 Python 时出现错误:

x = 1
def foo():
    x += 1

foo()
Run Code Online (Sandbox Code Playgroud)

UnboundLocalError: local variable 'x' referenced before assignment

我必须添加global x功能foo才能实现它。


似乎 python 让它更明确,是“只是为了明确”的原因吗?

c++ python global global-variables

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

函数内的 c++ using 语句,后跟函数名称(对于 ADL?)

什么是复制和交换习语? 在这个问题中,在最重要的答案中,在实现交换公共朋友重载的部分中,实现使用了这个:

friend void swap(dumb_array& first, dumb_array& second){
    //the line of code below
    using std::swap;
    //then it calls the std::swap function on data members of the dumb_array`s
}
Run Code Online (Sandbox Code Playgroud)

我的问题如下:using std::swap这里的用途是什么(答案提到了与启用 ADL 相关的内容);此处特别调用了“使用”的什么用例,添加该行代码的效果和不添加该代码行的效果是什么?

c++ swap

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