gpt4 book ai didi

c++ - 打包结构导致返回本地引用?

转载 作者:搜寻专家 更新时间:2023-10-31 02:17:10 25 4
gpt4 key购买 nike

我有以下代码,我正在努力理解为什么打包结构会导致警告(然后在运行时随后出现段错误。)首先是代码:

#include <iostream>

using namespace std;

template <typename T, std::size_t D, std::size_t S>
struct __attribute__((packed)) C {
union __attribute__((packed)) ValueType {
char fill[S];
T value;
};

ValueType data_;

const T& get() const {
return data_.value;
}

friend
std::ostream& operator<<(std::ostream& str, const C& f) {
return str << f.data_.value;
}
};

template <typename T, std::size_t D>
struct __attribute__((packed)) C<T, D, D> {
T value;

const T& get() const {
return value;
}

friend
std::ostream& operator<<(std::ostream& str, const C& f) {
return str << f.value;
}
};

template <typename T, std::size_t S = 0>
struct __attribute__((packed)) D {
C<T, sizeof(T), S> c;
const T& get() const {
return c.get();
}

friend
std::ostream& operator<<(std::ostream& str, const D& f) {
return str << f.get();
}
};

template <typename T>
struct __attribute__((packed)) D<T, 0> {
T v;
const T& get() const {
return v;
}

friend
std::ostream& operator<<(std::ostream& str, const D& f) {
return str << f.get();
}
};

int main(void) {
D<int64_t> d1;
cout << d1 << endl;
D<int64_t, 8> d2;
cout << d2 << endl;
}

通过打包(它是特定于 GCC 的)我得到以下内容(gcc 5.2.1/5.3.1,c++14 -Wall):

packed.cpp: In instantiation of ‘const T& D<T, 0ul>::get() const [with T = long int]’:
packed.cpp:58:16: required from ‘std::ostream& operator<<(std::ostream&, const D<long int>&)’
packed.cpp:64:13: required from here
packed.cpp:53:12: warning: returning reference to temporary [-Wreturn-local-addr]
return v;
^
packed.cpp: In instantiation of ‘const T& C<T, D, D>::get() const [with T = long int; long unsigned int D = 8ul]’:
packed.cpp:40:18: required from ‘const T& D<T, S>::get() const [with T = long int; long unsigned int S = 8ul]’
packed.cpp:45:16: required from ‘std::ostream& operator<<(std::ostream&, const D<long int, 8ul>&)’
packed.cpp:66:13: required from here
packed.cpp:27:12: warning: returning reference to temporary [-Wreturn-local-addr]
return value;

如果结构未打包 - 代码正常编译然后运行良好(打印垃圾值 - 但这是预期的。)

所以我的问题是 - 为什么打包会导致临时返回?

最佳答案

您要找的是pragma pack .它与您想要的效果相同,但更常见并且 cross-compiler .

注意:使用 pragma pack 添加代码的修改版本

#include <iostream>

using namespace std;

#pragma pack(1)
template <typename T, std::size_t D, std::size_t S>
struct C {
union ValueType {
char fill[S];
T value;
};

ValueType data_;

const T& get() const {
return data_.value;
}
};
#pragma pack()

template <typename T, std::size_t D>
struct C<T, D, D> {
T value;

const T& get() const {
return value;
}
};

template <typename T, std::size_t D>
struct C<T, D, 0> {
T value;

const T& get() const {
return value;
}
};

template <typename T, std::size_t S = 0>
struct D : C<T, sizeof(T), S> {
friend
std::ostream& operator<<(std::ostream& str, const D& f) {
return str << f.get();
}
};

#pragma pack(1)
struct E {
D<int16_t> a;
D<int64_t, 9> b;
};
#pragma pack()

int main(void) {
D<int16_t> d1;
cout << d1.get() << ' ' << sizeof(d1) << endl;
D<int64_t, 9> d2;
cout << d2.get() << ' ' << sizeof(d2) << endl;
cout << sizeof(E) << endl;
}

关于c++ - 打包结构导致返回本地引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36056474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com