Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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++_C Preprocessor_C++03 - Fatal编程技术网

C++ 预处理器:在结构名称中包含上一行号

C++ 预处理器:在结构名称中包含上一行号,c++,c-preprocessor,c++03,C++,C Preprocessor,C++03,我知道如何声明名称包含当前行号的结构。以下代码按预期工作 #define CREATE_NAME_CONCAT_(X, Y) X ## Y #define CREATE_NAME_CONCAT(X, Y) CREATE_NAME_CONCAT_(X, Y) #define CREATE_FOO_NAME CREATE_NAME_CONCAT(Foo_, __LINE__) struct CREATE_FOO_NAME { int x; }; typedef Foo_4 Foo; int main

我知道如何声明名称包含当前行号的结构。以下代码按预期工作

#define CREATE_NAME_CONCAT_(X, Y) X ## Y
#define CREATE_NAME_CONCAT(X, Y) CREATE_NAME_CONCAT_(X, Y)
#define CREATE_FOO_NAME CREATE_NAME_CONCAT(Foo_, __LINE__)
struct CREATE_FOO_NAME { int x; };
typedef Foo_4 Foo;
int main()
{
    Foo foo;
    foo.x = 42; 
    return 0;
}
如何使用前面的行号写入typedef行?以下代码无效:

#define CREATE_NAME_CONCAT_(X, Y) X ## Y
#define CREATE_NAME_CONCAT(X, Y) CREATE_NAME_CONCAT_(X, Y)
#define CREATE_FOO_NAME CREATE_NAME_CONCAT(Foo_, __LINE__)
struct CREATE_FOO_NAME { int x; };
typedef CREATE_NAME_CONCAT(Foo_, __LINE__-1) Foo;
int main()
{
    Foo foo;
    foo.x = 42; 
    return 0;
}
注1:是的,我有很好的理由这样做

注2:我不使用C++11或更高版本


注3:我不想讨论注1和注2。预处理器非常有限,最好的解决方案是根本不使用它。您可以使用模板完成类似的操作:

template <int>
struct FooT;
template <>
struct FooT<__LINE__> { int x; };
typedef FooT<__LINE__-1> Foo;

int main()
{
    Foo foo;
    foo.x = 42; 
    return 0;
}
模板
结构足;
模板
结构脚{intx;};
类型def FooT Foo;
int main()
{
富富,;
foo.x=42;
返回0;
}

我不知道这是否符合您的要求,因为这些要求似乎是保密的。

尽管如此请注意,默认情况下,它最多只能工作到
256
您想要实现什么?可能有更好的方法way@AlanBirtles:请看笔记……我确实读了你的笔记,但这并不意味着没有比尝试破解预处理器的限制更好的方法了,例如,数字模板参数将允许你进行所需的数学运算need@AlanBirtles:实际上,
BOOST\u PP\u DEC(\uuuuu LINE\uuuuuu)
而不是
\uuuuuuuuuuuu1
似乎在做这项工作。谢谢你的主意。