昨天我安装了clang 3.1和g ++ 4.7并尝试编译我正在研究的项目.我很惊讶地看到它没有使用两个编译器进行编译.但最令我惊讶的是,问题在于boost::shared_ptr.
显然,由于该类定义了一个移动构造函数/赋值运算符,因此隐式删除了复制构造函数.所以这段代码:
#include <boost/shared_ptr.hpp>
int main() {
boost::shared_ptr<int> x;
boost::shared_ptr<int> y(x);
}
Run Code Online (Sandbox Code Playgroud)
不编译.clang回应了这个错误:
test.cpp:5:28: error: call to implicitly-deleted copy constructor of
'boost::shared_ptr<int>'
boost::shared_ptr<int> y(x);
^ ~
/usr/include/boost/smart_ptr/shared_ptr.hpp:347:5: note: copy constructor is
implicitly deleted because 'shared_ptr<int>' has a user-declared move
constructor
shared_ptr( shared_ptr && r ): px( r.px ), pn() // never throws
^
Run Code Online (Sandbox Code Playgroud)
g ++ 4.7提供了类似的错误,指的是隐式删除的构造函数.奇怪的是boost::shared_ptr,实际上明确定义了一个复制构造函数(boost/smart_ptr/shared_ptr.hpp第228行):
template<class Y>
#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
#else …Run Code Online (Sandbox Code Playgroud) 当我执行以下代码时,我得到map/set迭代器不可递增的错误.
typedef std::multimap<int, int> MapType;
assgnt::MapType my_map;
assgnt::MapType::iterator it;
for(it = my_map.begin(); it != my_map.end(); )
{
my_map = obj1.addGoodNeighbours(it->first, it->second, my_map);
++it;
}
Run Code Online (Sandbox Code Playgroud)
请帮忙
使用C++ 11编译以下代码段(此处演示):
#include <stdint.h>
int main() {
const uint8_t foo[] = {
'\xf2'
};
}
Run Code Online (Sandbox Code Playgroud)
将触发(至少在GCC 4.7)的警告,表明有转换时的收缩转换'\xf2'到uint8_t.
为什么是这样?sizeof(char)总是1,应该是相同的sizeof(uint8_t),不应该吗?
请注意,当使用其他字符文字时'\x02',没有警告.
考虑以下代码(最小版本):
#include <iostream>
struct Base
{
virtual ~Base() {}
virtual void test() const { std::cout << "base"; }
};
struct Derived : public Base
{
void test() const { std::cout << "derived"; }
};
struct Composite
{
const Derived &ref;
Composite(const Derived &ref) : ref(ref) {}
void testRef() const { ref.test(); }
};
int main()
{
Composite c((Derived()));
c.testRef();
}
Run Code Online (Sandbox Code Playgroud)
当使用最新的MinGW g ++时,这实际上产生了"基础"!这是编译器错误还是我错过了什么?有人可以在VS中测试这个吗?
我认为自己是一位经验丰富的C++程序员,经常使用多态,基于堆栈的引用,临时对象(C++标准12.2)等.因此我知道应该应用终身延长.
只有在Base(虚拟或非虚拟)中定义析构函数并使用临时(即后续用法生成'derived')时,才会出现此行为:
int main()
{
Derived d;
Composite c(d);
c.testRef();
}
Run Code Online (Sandbox Code Playgroud) 有:
std::map<const int, float> m_areaCost;
Run Code Online (Sandbox Code Playgroud)
我正在尝试编译以下内容:
inline float getAreaCost(const int i) const {
return m_areaCost[i];
}
Run Code Online (Sandbox Code Playgroud)
这导致以下错误:
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)
我常想,当我们要求[elementId]我们获得元素值或默认的元素值,所以我不知道怎么能这么简单的情况下,会导致编译错误?
我想将一些协议作为类模板实现,其中raw_write和raw_read函数作为模板参数.两个原始函数都有严格定义的接口:
int raw_write(uint8_t *src, size_t len);
int raw_read(uint8_t *dst, size_t maxlen);
Run Code Online (Sandbox Code Playgroud)
当有人试图传递例如时,是否可以通过编译错误控制此接口:
int raw_write2(uint16_t *src, size_t len);
Run Code Online (Sandbox Code Playgroud)
我应该将模板参数作为指定类型的对象传递,还是作为在模板实现中实例化的类型名称?