【发布时间】:2018-06-21 22:49:31
【问题描述】:
我有这个main.cpp:
std::string hw[] = { "Hello", "World" };
const array_appender<std::string> ha( hw, sizeof( hw ) / sizeof( hw[ 0 ] ) );
if ( /*with some other conditions*/ &( ha.at( 0 ) ) == hw )
{
//some other stuff
}
我有这个模板:
template<typename T>
class array_appender {
public:
array_appender(T* array, size_t size) {
append(array, size);
}
void append(T* array, size_t size) {
for( int idx = 0; idx < size; ++idx)
{
std::cout << "value of array: " << array[idx] << std::endl;
data.add(array[idx]);
}
}
T at(size_t index) const {
return data[index];
}
size_t size() {
return data.size();
}
const size_t size() const {
return data.size();
}
private:
std::vector<T> data;
};
但由于上述情况,我得到了这个错误:
error: taking address of temporary [-fpermissive]
我搜索了这个,但在这种情况下,我找到的解决方案对我不起作用。感谢您的任何想法!
【问题讨论】:
-
这个
&( ha.at( 0 ) ) == hw应该做什么?你的at返回一个值(引用会更自然),那么通过获取它的地址会得到什么?