小编Osu*_*ldo的帖子

有人能告诉我为什么这不是一个持续的表达?

我正在尝试使用constexpr编写指向成员函数的链接列表.主要是为了好玩,但它可能有一个有用的应用程序.

struct Foo;

using MethodPtr = void (Foo::*)();

struct Node
{
    constexpr Node(MethodPtr method, const Node* next)
        : Method(method)
        , Next(next)
    {}

    constexpr Node Push(MethodPtr method)
    {
        return Node(method, this);
    }

    MethodPtr Method;
    const Node* Next;
};

struct Foo
{
    constexpr static Node GetMethods()
    {
        return Node{&Foo::Method1, nullptr}
            .Push(&Foo::Method2)
            .Push(&Foo::Method3);
    }

    void Method1() {}
    void Method2() {}
    void Method3() {}
};

int main(void)
{
    constexpr Node node = Foo::GetMethods();
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在调用GetMethods()时主要给出了以下错误:

const Node{MethodPtr{Foo::Method3, 0}, ((const Node*)(& Node{MethodPtr{Foo::Method2, 0}, ((const Node*)(& …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr c++11

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

反向字符串函数给我一个分段错误

我正在研究我的C/C++技能.我试图实现一个反转字符串的函数,但每次运行程序时我都会遇到分段错误(核心转储).

#include <stdio.h>
#include <string.h>

void revstr(char *str);

int main()
{
    char *str = "hello mofo!";
    revstr(str);
    puts(str);

    return 0;
}

void revstr(char *str)
{
    int start = 0;
    int len = strlen(str);
    int mid = len / 2;
    int i, t;
    printf("start: %d, mid: %d,len: %d\n", start, mid, len);

    for ( i = start; i < mid; ++i )
    {
        printf("str[%d] swapping to str[%d]: %c, %c\n", i, len - 1 -i, str[i], str[len - 1 - i]);
        t = …
Run Code Online (Sandbox Code Playgroud)

c++ segmentation-fault

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

标签 统计

c++ ×2

c++11 ×1

constexpr ×1

segmentation-fault ×1