小编Mor*_*enn的帖子

与constexpr陷入困境

我在使用常量初始化类时遇到了麻烦:

为什么使用指向同一类中成员的指针初始化会导致错误?出现错误而不使用"使用"类!

class A
{   
    private:
        int a;
        const int* const aptr;

    public:
        constexpr A( int _a):
            a(_a)
           , aptr( &a)           // why aptr could not be initialized? 
    {}  
};  

class Data { } d1; 

class B
{   
    private:
        Data* dptr1;

    public:
        constexpr B(Data* _p): dptr1( _p) {}

};  

class Use 
{   
    static constexpr A a{2};   // fail! error: field initializer is not constant
    static constexpr B b{&d1}; // works
};  
Run Code Online (Sandbox Code Playgroud)

c++ constexpr c++11

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

constexpr将十六进制字符转换为std :: string

我有很多像这样的字符串:

"343536"_hex
Run Code Online (Sandbox Code Playgroud)

我想将其转换为相应的字节字符串.我正在使用C++ 11并定义了一个用户定义的字符串文字,将它们转换为十六进制字符串.但是,我目前的转换不能被评估为constexpr我正在寻求的.特别是我想使用这样的东西,但作为一个constexpr:

std::string operator "" _hex(const char *s, std::size_t slen )
{
    std::string str;
    str.reserve(slen);
    char ch[3];
    unsigned long num;
    ch[2] = '\0';

    for ( ; slen; slen -= 2, s += 2) {
        ch[0] = s[0];
        ch[1] = s[1];
        num = strtoul(ch, NULL, 16);
        str.push_back(num);
    }
    return str;
}
Run Code Online (Sandbox Code Playgroud)

测试司机

int main()
{
    std::string src{"653467740035"_hex};
    for (const auto &ch : src)
        std::cout << std::hex << std::setw(2) << std::setfill('0') 
                  << (unsigned)ch << '\n';
} …
Run Code Online (Sandbox Code Playgroud)

c++ string type-conversion c++11

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

块排序算法

从维基百科的块排序页面我发现块排序的工作原理是将初始数组划分为长度为 16 的小子数组,例如,在 O(n) 时间内对所有这些子数组进行排序,然后以我可以的方式合并所有这些块不明白。

例如,考虑一个长度为 16 的数组,将其分成 4 个块,每个块的长度为 4,并对这些块进行排序,我们得到:

10 1 8 3 4 19 20 13 14 17 8 9 12 18 7 20
10 1 8 3 ----- 4 19 20 13 ----- 14 17 8 9 ----- 12 18 7 20
1 3 8 10 ----- 4 13 19 20 ----- 8 9 14 17 ----- 7 12 18 20
Run Code Online (Sandbox Code Playgroud)

谁能解释一下合并步骤是如何工作的?

sorting mergesort

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

get_includes 找不到标准库头文件

我正在玩弄 libclang 的 Python 绑定。目前,我正在尝试执行一些非常简单的任务,例如查找 C++ 文件中包含的所有标头。我使用的代码如下:

from clang.cindex import Index

index = Index.create()
tu = index.parse("hello.cpp", args=["-std=c++14"])
for it in tu.get_includes():
    print(it.include.name)
Run Code Online (Sandbox Code Playgroud)

该文件hello.cpp如下:

#include <iostream>
#include <stdio.h>
#include "hello.h"

int main()
{
    std::cout << "Hello world\n";
}
Run Code Online (Sandbox Code Playgroud)

文件hello.h如下:

#include <list>
Run Code Online (Sandbox Code Playgroud)

我认为上面的代码会打印iostream, stdio.hand hello.h,list如果考虑到可传递的包含,可能甚至更多。但是,它只打印./hello.h,公然忽略标准库头文件。

我在文档中找不到任何关于它是否是设计的。是设计的吗?如果是这样,有什么方法可以实际获取文件clang.cindex包含的所有标头,包括标准库的标头?

c++ python libclang

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

libvlc:如何随时捕捉帧

现在我的方法是:

libvlc_video_set_callbacks //set the lock,unlock,display callback functions
libvlc_media_player_set_position //goto the snap time
libvlc_media_player_play  //start playing
Run Code Online (Sandbox Code Playgroud)

然后在解锁回调函数中调用'libvlc_media_player_stop'停止播放,并通过显示回调函数保存提供者的数据。

这种方法已经成功了几次,但更多时候会使我的整个程序崩溃。我知道它看起来有点愚蠢,但我在libvlc包含文件中找不到任何逐帧控制功能。

c libvlc

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

为什么不能-O0禁用gcc编译优化?

string str="fujian";
Run Code Online (Sandbox Code Playgroud)

有些书说代码会触发复制构造函数,但是g ++会对它进行优化,以便不会调用复制构造函数.

但是,我使用g ++命令-O0来禁用优化,但它仍然无法触发复制构造函数.

怎么理解呢?

c++ string g++

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

模板演绎似乎有误

推导出的模板似乎是错误的,为什么(c)被调用而不是(b)?

#include <iostream>
using namespace std;
template<class T> void f(T){cout << "f(T)";}//(a)
template<> void f<>(int*){cout << "f(int*)";}//(b)
template<class T> void f(T*){cout << "f(T*)";}//(c)
//void f(int*){cout <<"POD:f(int*)";}//(d)

int main(int argc,char*argv[])
{
    int p = 1;
    f(&p);
    cout <<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

f(T*)

c++ templates

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

初始化包含带有lambda的std :: function的类

我创建了一个模板类,其中包含std::function一个成员,方法如下:

template<typename Ret, typename... Args>
class Foo
{
private:
    std::function<Ret(Args...)> _func;

public:
    Foo(const std::function<Ret(Args...)>& func):
        _func(func)
    {}
};
Run Code Online (Sandbox Code Playgroud)

为了不必指定传递函数的参数和返回类型,我创建了一些make_foo重载:

template<typename Ret, typename... Args>
auto make_foo(Ret (&func)(Args...))
    -> Foo<Ret, Args...>
{
    return { std::function<Ret(Args...)>(func) };
}

template<typename Ret, typename... Args>
auto make_foo(const std::function<Ret(Args...)>& func)
    -> Foo<Ret, Args...>
{
    return { func };
}
Run Code Online (Sandbox Code Playgroud)

但是,我无法创建一个make_foo以lambda作为参数的重载:

template<typename Ret, typename... Args>
auto make_foo(??? func)
    -> Foo<Ret, Args...>
{
    return { std::function<Ret(Args...)>(func) };
}
Run Code Online (Sandbox Code Playgroud)

我只是找不到从lambda自动推导出返回类型和参数类型的方法.有没有一种解决这种问题的惯用方法?

c++ lambda templates c++11 std-function

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

别名模板的部分专业化

不允许使用别名模板的部分特化:

例如,尝试创造性,在clang中产生此错误:

template <typename T>
using unwrapped_future_t = T;

template <typename T>
using unwrapped_future_t<future<T>> = typename future<T>::value_type;
                        ^~~~~~~~~~~
> error: partial specialization of alias templates is not permitted
Run Code Online (Sandbox Code Playgroud)

为什么不允许这样做?

c++ templates partial-specialization template-specialization c++11

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

CRTP类中成员函数的可见性

我正在编写一个带有分拣机功能对象的分类库.其中一个主要类别sorter_facade旨在operator()根据已存在的重载为分拣机提供一些重载.这是一个简单的heap_sorter对象简化示例,实现了一个heapsort:

struct heap_sorter:
    sorter_facade<heap_sorter>
{
    using sorter_facade<heap_sorter>::operator();

    template<typename Iterator>
    auto operator()(Iterator first, Iterator last) const
        -> void
    {
        std::make_heap(first, last);
        std::sort_heap(first, last);
    }
};
Run Code Online (Sandbox Code Playgroud)

最简单的目标之一sorter_facade是在operator()已经存在一对迭代器的重载时为分类器提供可迭代的重载.这是一个简化的实现sorter_facade,足以解决手头的问题:

template<typename Sorter>
struct sorter_facade
{
    template<typename Iterable>
    auto operator()(Iterable& iterable) const
        -> std::enable_if_t<
            not has_sort<Sorter, Iterable>,
            decltype(std::declval<Sorter&>()(std::begin(iterable), std::end(iterable)))
        >
    {
        return Sorter{}(std::begin(iterable), std::end(iterable));
    }
};
Run Code Online (Sandbox Code Playgroud)

在这个类中,has_sort是一个特征,用于检测分拣机是否有operator()过载Iterable&.它是使用检测成语的手动版本实现的:

template<typename Sorter, typename Iterable>
using has_sort_t = std::result_of_t<Sorter(Iterable&)>;

template<typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates crtp language-lawyer c++14

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