编译boost :: program_options的示例代码:http://svn.boost.org/svn/boost/trunk/libs/program_options/example/first.cpp
...在MacOS Lion(10.7.2)上,使用随MacPorts安装的boost-1.48.0:
$ clang++ -v
Apple clang version 3.0 (tags/Apple/clang-211.12) (based on LLVM 3.0svn)
Target: x86_64-apple-darwin11.2.0
Thread model: posix
$ clang++ -std=c++0x --stdlib=libc++ -lc++ -I/opt/local/include -L/opt/local/lib -lboost_program_options first.cpp -o first
Undefined symbols for architecture x86_64:
"boost::program_options::options_description::options_description(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned int, unsigned int)", referenced from:
_main in cc-6QQcwm.o
"boost::program_options::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, boost::program_options::options_description const&)", referenced from:
_main in cc-6QQcwm.o
"boost::program_options::abstract_variables_map::operator[](std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:
boost::program_options::variables_map::operator[](std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const in …Run Code Online (Sandbox Code Playgroud) 这个问题的答案为什么不能用c ++ 0x模式中的libc ++来扼制这个boost :: program_options例子?陈述"你需要使用clang ++ -stdlib = libc ++重建boost."
我正在使用带有clang v3.0的MacOS Lion.如何使用clang构建Boost v1.48.0并将其与libc ++链接?
更新:我已经创建了一个user-config.jam文件,其中包含以下内容:
using clang-darwin
Run Code Online (Sandbox Code Playgroud)
...将使用clang而不是gcc构建Boost.如何链接libc ++而不是libstdc ++?
注意派生使用C++ 11统一初始化语法来调用基类构造函数.
class base
{
protected:
base()
{}
};
class derived : public base
{
public:
derived()
: base{} // <-- Note the c++11 curly brace syntax
// using uniform initialization. Change the
// braces to () and it works.
{}
};
int main()
{
derived d1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++ 4.6编译它,但是g ++ 4.7没有:
$ g++-4.7 -std=c++11 -Wall -Wextra -pedantic curly.cpp -o curly
curly.cpp: In constructor ‘derived::derived()’:
curly.cpp:4:13: error: ‘base::base()’ is protected
curly.cpp:19:24: error: within this context …Run Code Online (Sandbox Code Playgroud) 如何让这个简单的类移动?我认为是正确的只会产生一堵错误之墙......
#include <iostream>
#include <sstream>
#include <utility>
class message
{
public:
message() = default;
// Move constructor
message( message &&other ) :
stream_( std::move( other.stream_ ) ) // Nope
{}
// Move assignment
message &operator=( message &&other )
{
if ( this != &other )
{
stream_ = std::move( other.stream_ ); // Nope #2
}
return *this;
}
private:
message( const message & ) = delete;
message &operator=( const message & ) = delete;
std::stringstream stream_;
// Other member variables …Run Code Online (Sandbox Code Playgroud) 而不是......
int value = get_value();
if ( value > 100 )
{
// Do something with value.
}
Run Code Online (Sandbox Code Playgroud)
......是否可以将价值范围缩小到只需要的地方:
if ( int value = get_value() > 100 )
{
// Obviously this doesn't work. get_value() > 100 returns true,
// which is implicitly converted to 1 and assigned to value.
}
Run Code Online (Sandbox Code Playgroud) 我auto几乎到处都使用c ++ 11 关键字.我不确定在这种情况下我是否正确使用它.请考虑以下简单示例:(http://ideone.com/TxLJlx)
#include <iostream>
const char* f()
{
return "Hello";
}
int main()
{
auto s1 = f();
auto* s2 = f();
std::cout << s1 << std::endl;
std::cout << s2 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
双方auto并auto*似乎工作,似乎做同样的事情.这个假设是错的吗?
为什么两者都给出相同的结果?
auto在这种情况下哪个是正确的用法?
在下面的代码中,为什么在使用gcc编译时,在Linux和Windows上打包结构的大小是不同的?
#include <inttypes.h>
#include <cstdio>
// id3 header from an mp3 file
struct header
{
uint8_t version[ 2 ];
uint8_t flags;
uint32_t size;
} __attribute__((packed));
int main( int argc, char **argv )
{
printf( "%u\n", (unsigned int)sizeof( header ) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用的gcc版本:
$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
$ x86_64-w64-mingw32-g++ --version
x86_64-w64-mingw32-g++ (GCC) 4.7.0 20110831 (experimental)
Run Code Online (Sandbox Code Playgroud)
编译和测试:
$ g++ -Wall packed.cpp -o packed && ./packed
7
$ x86_64-w64-mingw32-g++ -Wall packed.cpp -o packed.exe
--> prints '8' when run …Run Code Online (Sandbox Code Playgroud) 我有一个QTialog,用QT Designer创建,看起来像这样: 
左侧的服务器列表是带有QStringListModel的QListView.通过将视图的激活(QModelIndex)信号连接到对话框中的插槽功能,鼠标单击列表视图中的项目将使用所选项目的信息更新表单.
但是,向上或向下按键盘也会更改所选项目,但不会发出任何信号,因此表单不会更新以匹配所选项目.怎么解决这个问题?
由于Visual Studio 2013中的这个错误,我需要提供自己的移动构造函数并移动派生类的赋值.但是,我不知道如何为基类调用适当的移动函数.
这是代码:
#include <utility>
// Base class; movable, non-copyable
class shader
{
public:
virtual ~shader()
{
if (id_ != INVALID_SHADER_ID)
{
// Clean up
}
}
// Move assignment
shader& operator=(shader&& other)
{
// Brett Hale's comment below pointed out a resource leak here.
// Original:
// id_ = other.id_;
// other.id_ = INVALID_SHADER_ID;
// Fixed:
std::swap( id_, other.id_ );
return *this;
}
// Move constructor
shader(shader&& other)
{
*this = std::move(other);
}
protected:
// Construct an …Run Code Online (Sandbox Code Playgroud) 目前 在 已经 问题在这里问#1 为什么 basic_fstream<uint8_t>不工作.答案说这char_traits只是专门用于char和wchar_t(加上char16_t,char32_t在C++ 11中),你应该坚持basic_fstream<char>阅读二进制数据并在需要时投射它.
好吧,这还不够好!:)
没有任何答案(我能找到)说明如何专门化char_traits<uint8_t>和使用basic_fstream模板,或者甚至可能.所以我想我会尝试自己实现它.
在Windows 7 64位上使用Visual Studio Express 2013 RC并在Kubuntu GNU/Linux 13.04 64位上使用g ++ - 4.7时,以下编译没有错误.但是它会在运行时抛出std :: bad_cast异常.我没有访问clang ++和libc ++来测试这个组合.
#include <cinttypes>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <locale>
#ifdef _WIN32
#define constexpr
#define NOEXCEPT throw()
#else
#define NOEXCEPT noexcept
#endif
// Change this to char and it works.
using byte_type …Run Code Online (Sandbox Code Playgroud)