C/C++后加1以上

发布于 2024-10-24 17:01:57 字数 506 浏览 1 评论 0原文

我正在从缓冲区读取字节。但有时我读的只是一个词或更长。

// assume buffer is of type unsigned char *
read_ptr(buffer+(position++))

没问题,但是我怎样才能将位置后增加 2 或 4 呢?我无法让 += 运算符进行后递增,是吗?

原因是,我有一个大而可怕的表达式,我想对其进行评估,同时递增位置变量。

我想我想出了自己的解决方案。我很确定它有效。但每个人都会讨厌它,因为这不是非常可读的代码。

read_ptr(buffer+(position+=4)-4)

在对其进行一些测试以确保它做正确的事情之后,我将把它变成一个宏。

结论:

不要这样做。这只是一个坏主意,因为这会生成无法维护的代码。但是......事实证明,将任何预递增运算符转换为后递增运算符确实非常容易。

I'm reading bytes from a buffer. But sometimes what I'm reading is a word or longer.

// assume buffer is of type unsigned char *
read_ptr(buffer+(position++))

That's fine but how can I post-increment position by 2 or 4? There's no way I can get the += operator to post-increment, is there?

Reason is, I have this big awful expression which I want to evaluate, while at the same time incrementing the position variable.

I think I came up with my own solution. I'm pretty sure it works. Everyone's gonna hate it though, since this isn't very readable code.

read_ptr(buffer+(position+=4)-4)

I will then make this into a macro after testing it a bit to make sure it's doing the right thing.

IN CONCLUSION:

Don't do this. It's just a bad idea because this is the sort of thing that generates unmaintainable code. But... it does turn out to be quite easy to convert any pre-incrementing operator into a post-incrementing one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

所谓喜欢 2024-10-31 17:01:57

如何将 position 后增加 2 或 4?

您不能将变量后递增 2 或 4,但您可以使用以下内容(根据您的情况)

read_ptr(buffer+position);位置 += 2;

how can I post-increment position by 2 or 4?

You can't post-increment a variable by 2 or 4 but you can use the following (in your case)

read_ptr(buffer+position); position += 2;

口干舌燥 2024-10-31 17:01:57

虽然,我不推荐这个解决方案,但如果您不想更改代码中的这一行:

read_ptr(buffer+(position++));

并且您仍然想将 position 后置 2,则定义 position< /em> as Index position(2); 其中类型 Index 在此定义,并且还显示了用法:

struct Index
{
    int step;
    int value;
    Index(int s=1, int v=0): step(s), value(v) {}
    Index operator++(int) 
    { 
       Index prev(step, value); 
       value += step; 
       return prev;
    }
    operator int() { return value; }
};

int main() {
        char arr[] = "1234567890" ;

        cout <<"Increment by 2" <<endl;
        Index i2(2); //increment by 2
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;

        cout <<"Increment by 3" <<endl;        
        Index i3(3); //increment by 3
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        return 0;
}

输出:

Increment by 2
1
3
5
7
Increment by 3
1
4
7
0

工作示例:http://ideone.com/CFgal

注意: 我仍然不会在现实项目中建议这种解决方案。这更像是拼图 :D

Although, I would not recommend this solution, but if you don't want to change this line in your code:

read_ptr(buffer+(position++));

And you still want to post-increment position by 2, then define position as Index position(2); where the type Index is defined here, and also shown the usage:

struct Index
{
    int step;
    int value;
    Index(int s=1, int v=0): step(s), value(v) {}
    Index operator++(int) 
    { 
       Index prev(step, value); 
       value += step; 
       return prev;
    }
    operator int() { return value; }
};

int main() {
        char arr[] = "1234567890" ;

        cout <<"Increment by 2" <<endl;
        Index i2(2); //increment by 2
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;

        cout <<"Increment by 3" <<endl;        
        Index i3(3); //increment by 3
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        return 0;
}

Output:

Increment by 2
1
3
5
7
Increment by 3
1
4
7
0

Working Example : http://ideone.com/CFgal

Note: I would still not suggest this solution in real life project. It's more like puzzle :D

为你鎻心 2024-10-31 17:01:57

+= 运算符将是一个单独的语句(不是后增量或预增量)。您可以使用以下行:

func(buffer + position); position += 2;

The += operator would be a separate statement (not post or pre increment). You could use the following line:

func(buffer + position); position += 2;
深居我梦 2024-10-31 17:01:57

你不知道;你把它分成多行。没有理由将所有内容都塞到这里的一行中。

read_ptr( buffer + position );
position += n;

You don't; you break it up into more than one line. There is no reason to stuff everything into one line here.

read_ptr( buffer + position );
position += n;
梦断已成空 2024-10-31 17:01:57

好吧,我确实在编辑中回答了我的问题...基本上我想要的是一个单个表达式,它计算原始值,但具有任意增量的副作用。这是一些宏。

#define INC(x,inc) (((x)+=(inc))-(inc))
#define INC2(x) INC(x,2)
#define INC4(x) INC(x,4)
#define INC8(x) INC(x,8)

Well, I did answer my question in the edit... Basically what I wanted was a single expression which evaluates to the original value but has a side effect of incrementing by an arbitrary amount. Here are some macros.

#define INC(x,inc) (((x)+=(inc))-(inc))
#define INC2(x) INC(x,2)
#define INC4(x) INC(x,4)
#define INC8(x) INC(x,8)
萤火眠眠 2024-10-31 17:01:57

如果 position 是指向 int16int32 的指针,则递增它会分别加 2 或 4。

If position were a pointer to int16 or int32, incrementing it would add 2 or 4, respectively.

生生漫 2024-10-31 17:01:57

在 C++ 中,您可以轻松编写一个函数来执行后置式双增量:

template <typename T>
T inc2(T &t) {
    T r(t);
    ++t; // or t++ if you want to respect inconsistently-overloaded operators,
    ++t; // but I wouldn't bother.
    return r;
}

read_ptr(buffer+inc2(position))

在 C 中,情况稍微尴尬一些:

size_t inc2(size_t *s) { // or whatever type you're using
    size_t r = *s;
    (*s) += 2;
    return r;
}

read_ptr(buffer+inc2(&position))

您也可以通过将其作为附加函数参数或在C++案例。

还有第二个问题,是否值得在 C++ 或 C 中追求这种编程风格,在 C 中,您可以在一条语句中完成这么多工作。避免副作用可以使代码更容易理解,即使代码更长。

In C++, you can easily write a function to perform a post-style double-increment:

template <typename T>
T inc2(T &t) {
    T r(t);
    ++t; // or t++ if you want to respect inconsistently-overloaded operators,
    ++t; // but I wouldn't bother.
    return r;
}

read_ptr(buffer+inc2(position))

In C it's slightly more awkward:

size_t inc2(size_t *s) { // or whatever type you're using
    size_t r = *s;
    (*s) += 2;
    return r;
}

read_ptr(buffer+inc2(&position))

You can cover the 4 case as well by making it an additional function parameter, or perhaps an additional template parameter in the C++ case.

There's a second question, whether it's worth pursuing this style of programming in C++ or in C, where you do so much in a single statement. Avoiding side-effects can make the code easier to understand, even though it comes out longer.

南汐寒笙箫 2024-10-31 17:01:57

Steve Jessop 的答案的稍微更通用的版本接受所有类型的增量步骤。

template <auto Step>
constexpr auto post_increment(auto& lvalue)
{
    auto copy = lvalue;
    lvalue += Step;
    return copy;
}

如果您明确需要调用 operator++ 而不是 operator+=

template <auto I>
constexpr auto post_increment_helper(auto& lvalue)
{
    lvalue++; // or ++lvalue
    if constexpr (I > 1)
        post_increment_helper<I - 1>(lvalue);
}

template <auto Step>
constexpr auto post_increment(auto& lvalue)
{
    auto copy = lvalue;
    post_increment_helper<Step>(lvalue);
    return copy;
}

应用程序:

int a = 4;
auto value = post_increment<5>(a);
std::cout << a << ' ' << value << '\n'; // prints 9 4

A slightly more generalized version of Steve Jessop's answer to accept all kind of increment steps.

template <auto Step>
constexpr auto post_increment(auto& lvalue)
{
    auto copy = lvalue;
    lvalue += Step;
    return copy;
}

If you explicity need to call operator++ instead of operator+= :

template <auto I>
constexpr auto post_increment_helper(auto& lvalue)
{
    lvalue++; // or ++lvalue
    if constexpr (I > 1)
        post_increment_helper<I - 1>(lvalue);
}

template <auto Step>
constexpr auto post_increment(auto& lvalue)
{
    auto copy = lvalue;
    post_increment_helper<Step>(lvalue);
    return copy;
}

Application :

int a = 4;
auto value = post_increment<5>(a);
std::cout << a << ' ' << value << '\n'; // prints 9 4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文