Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;-递归结构-可能吗? 我试图在C++中实现递归结构,看起来应该有点类似: typedef struct { static constexpr int foo() { return 1; } typedef struct { // not valid - I meant foo() from "type" not from "recursive_type" static constexpr int foo() { return 2 * foo(); } // ? (there should be another recursive type here) } recursive_type; } type; static_assert(type::foo() == 1, "Nope"); static_assert(type::recursive_type::foo() == 2, "Nope"); static_assert(type::recursive_type::recursive_type::foo() == 4, "Nope");_C++_Recursion - Fatal编程技术网

C++;-递归结构-可能吗? 我试图在C++中实现递归结构,看起来应该有点类似: typedef struct { static constexpr int foo() { return 1; } typedef struct { // not valid - I meant foo() from "type" not from "recursive_type" static constexpr int foo() { return 2 * foo(); } // ? (there should be another recursive type here) } recursive_type; } type; static_assert(type::foo() == 1, "Nope"); static_assert(type::recursive_type::foo() == 2, "Nope"); static_assert(type::recursive_type::recursive_type::foo() == 4, "Nope");

C++;-递归结构-可能吗? 我试图在C++中实现递归结构,看起来应该有点类似: typedef struct { static constexpr int foo() { return 1; } typedef struct { // not valid - I meant foo() from "type" not from "recursive_type" static constexpr int foo() { return 2 * foo(); } // ? (there should be another recursive type here) } recursive_type; } type; static_assert(type::foo() == 1, "Nope"); static_assert(type::recursive_type::foo() == 2, "Nope"); static_assert(type::recursive_type::recursive_type::foo() == 4, "Nope");,c++,recursion,C++,Recursion,这应该是这样的: typedef struct { static constexpr int foo() { return 1; } typedef struct { // not valid - I meant foo() from "type" not from "recursive_type" static constexpr int foo() { return 2 * foo();

这应该是这样的:

typedef struct {
    static constexpr int foo() {
        return 1;
    }
    typedef struct {
        // not valid - I meant foo() from "type" not from "recursive_type"
        static constexpr int foo() {
            return 2 * foo(); 
        }
        // ? (there should be another recursive type here)
    } recursive_type;
} type;
static_assert(type::foo() == 1, "Nope");
static_assert(type::recursive_type::foo() == 2, "Nope");
static_assert(type::recursive_type::recursive_type::foo() == 4, "Nope");
基本上-我希望
recursive\u type
包含与
type
完全相似的结构,但其
foo()
返回的值是
type
foo()
值的两倍。但正如我在评论中指出的,我的方法有几个问题,不幸的是,它不起作用


这种结构是否可以在C++中以某种方式声明,或者可能不可能?

排序。这是C++中实现类型递归的方式。

template< int tag >
struct X
{
    static constexpr int foo() { return 2 * X<tag-1>::foo(); }
};

template< >
struct X<1>
{
    static constexpr int foo() { return 1; }
};

#include <iostream>
using namespace std;

int main()
{
    static_assert(X<1>::foo() == 1, "Nope");
    static_assert(X<2>::foo() == 2, "Nope");
    static_assert(X<3>::foo() == 4, "Nope");

    cout << X<10>::foo() << endl;
}
模板
结构X
{
静态constexpr int foo(){return 2*X::foo();}
};
模板<>
结构X
{
静态constexpr int foo(){return 1;}
};
#包括
使用名称空间std;
int main()
{
静态_断言(X::foo()==1,“Nope”);
静态_断言(X::foo()==2,“Nope”);
静态_断言(X::foo()==4,“Nope”);

CUT< P>排序。这是C++中实现类型递归的方式。

template< int tag >
struct X
{
    static constexpr int foo() { return 2 * X<tag-1>::foo(); }
};

template< >
struct X<1>
{
    static constexpr int foo() { return 1; }
};

#include <iostream>
using namespace std;

int main()
{
    static_assert(X<1>::foo() == 1, "Nope");
    static_assert(X<2>::foo() == 2, "Nope");
    static_assert(X<3>::foo() == 4, "Nope");

    cout << X<10>::foo() << endl;
}
模板
结构X
{
静态constexpr int foo(){return 2*X::foo();}
};
模板<>
结构X
{
静态constexpr int foo(){return 1;}
};
#包括
使用名称空间std;
int main()
{
静态_断言(X::foo()==1,“Nope”);
静态_断言(X::foo()==2,“Nope”);
静态_断言(X::foo()==4,“Nope”);

可以是的,借用Let_Me__Be你可以得到你要求的行为:

template< int tag >
struct X
 {
    static constexpr int foo() { return 2 * X<tag-1>::foo(); }

     typedef X<tag+1> recursive_type;
};


template< >
struct X<0>
{
    static constexpr int foo() { return 1; }

    typedef X<1> recursive_type;
};

typedef X<0> type;

static_assert(type::foo() == 1, "Nope");
static_assert(type::recursive_type::foo() == 2, "Nope");
static_assert(type::recursive_type::recursive_type::foo() == 4, "Nope");
模板
结构X
{
静态constexpr int foo(){return 2*X::foo();}
typedef X递归_类型;
};
模板<>
结构X
{
静态constexpr int foo(){return 1;}
typedef X递归_类型;
};
类型def X类型;
静态_断言(type::foo()==1,“Nope”);
静态断言(类型::递归类型::foo()==2,“Nope”);
静态断言(类型::递归类型::递归类型::foo()==4,“Nope”);

当然,你还可以将
递归类型
作为
X
编写深入的递归使用…

是的,借用Let\u Me\u Be,你可以得到你想要的行为:

template< int tag >
struct X
 {
    static constexpr int foo() { return 2 * X<tag-1>::foo(); }

     typedef X<tag+1> recursive_type;
};


template< >
struct X<0>
{
    static constexpr int foo() { return 1; }

    typedef X<1> recursive_type;
};

typedef X<0> type;

static_assert(type::foo() == 1, "Nope");
static_assert(type::recursive_type::foo() == 2, "Nope");
static_assert(type::recursive_type::recursive_type::foo() == 4, "Nope");
模板
结构X
{
静态constexpr int foo(){return 2*X::foo();}
typedef X递归_类型;
};
模板<>
结构X
{
静态constexpr int foo(){return 1;}
typedef X递归_类型;
};
类型def X类型;
静态_断言(type::foo()==1,“Nope”);
静态断言(类型::递归类型::foo()==2,“Nope”);
静态断言(类型::递归类型::递归类型::foo()==4,“Nope”);

当然,还有一个好处,那就是你可以把
recursive\u type
写成
X

你最好使用指针,也许是智能指针。并且避免命名两个
struct
(在同一个翻译单元中)使用相同的名称。是的,我知道我可以使用指针来做这件事,但我需要一个语义与我描述的相似的对象。(因此,
recursive\u type
应该生成它所调用的对象的稍微修改的版本)你是否考虑切换到OcAML?它有递归的函数。你最好使用指针,也许是智能的。避免用相同的名字命名两个代码> Stutt(在同一个翻译单元中)。是的,我知道我可以用指针来做这件事,但是我需要一个对象,它的语义看起来和我描述的一样。(所以<代码> RealsiviyType < /Cord>应该生成它被调用的对象的稍微修改版本)。您是否考虑切换到OCAML?它具有递归函子…