有没有到底应该用来代替自由函数来解释,当容器开始和特定版本的任何一般的偏好或规则std::begin和std::end?
这是我的理解是,如果该函数是一个模板,其中容器类型是模板参数则std::begin和std::end应使用,即:
template<class T> void do_stuff( const T& t )
{
std::for_each( std::begin(t), std::end(t), /* some stuff */ );
}
Run Code Online (Sandbox Code Playgroud)
在其他场景中,例如已知容器类型的标准/成员函数呢?它仍然是更好的做法是使用std::begin(cont)和std::end(cont)或应容器的成员函数cont.begin()和cont.end()首选?
我在假设有通过调用在性能上没有任何好处纠正cont.end()过std::end(cont)?
该标准提供了一个模板特化std::unique_ptr,正确调用delete[]其析构函数:
void func()
{
std::unique_ptr< int[] > arr(new int[10]);
.......
}
Run Code Online (Sandbox Code Playgroud)
由于std::shared_ptr这种专业化不可用,因此有必要提供一个正确调用的删除器delete[]:
void func()
{
// Usage
shared_ptr array (new double [256], [](double* arr) { delete [] arr; } );
..............
}
Run Code Online (Sandbox Code Playgroud)
这只是一个疏忽吗?(以同样的方式存在std::copy_if)或是否有原因?
是否有关于如何使用Doxygen记录C++模板和模板元函数的指南?
例如:
/// @brief metafunction for generation of a map of message types to
/// their associated callbacks.
/// @tparam Seq the list of message types
template< class Seq >
struct generate_callback_map
{
typedef typename mpl::transform< Seq
, build_type_signature_pair< mpl::_1 >
>::type vector_pair_type;
typedef typename fusion::result_of::as_map< vector_pair_type >::type type;
};
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经看到了以下建议:
@tparam 用于记录模板参数.@arg 记录模板参数的替代方法.@brief 用于描述元功能.如何记录元函数的"返回类型"?
有没有人对使用Doxygen和C++模板有任何好的建议或个人喜好?
我正在尝试调整其中一个boost :: asio示例,尽可能使用c ++ 11/TR1库.原始代码如下所示:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}
Run Code Online (Sandbox Code Playgroud)
如果我更换boost::bind与std::bind如下:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
acceptor_.async_accept(new_connection->socket(),
std::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error ) );
// std::bind(&tcp_server::handle_accept, this, new_connection, _1 ) );
}
Run Code Online (Sandbox Code Playgroud)
我收到一条大错误消息,结尾为:
/usr/include/c++/4.4/tr1_impl/functional:1137: error: return-statement with a value, in function returning 'void'
Run Code Online (Sandbox Code Playgroud)
我使用的是增强版1.47的gcc 4.4版
我希望boost :: bind和std :: bind可以互换.
在switch语句中使用强类型枚举时有没有办法避免显式转换int?
/// @desc an enumeration of the states that the session can be in.
enum class State
{
Created,
Connected,
Active,
Closed
};
State sesState = session->GetState();
switch (static_cast<int>(sesState))
{
case static_cast<int>(Session::State::Created):
// do stuff.
break;
case static_cast<int>(Session::State::Connected):
// do stuff.
break;
}
Run Code Online (Sandbox Code Playgroud)
从n3242草案:
6.4.2 switch语句[stmt.switch]
2条件应为整数类型,枚举类型或类型,其中存在单个非显式转换函数为积分或枚举类型(12.3).
枚举类型是否包含强类型枚举,或者它们是否与switch语句不兼容,因为它们需要显式转换为int?
继我对此做出的评论之后:
将std :: vector传递给构造函数并移动语义
是否std::move在下面的代码中是必要的,以确保返回的值是xvalue?
std::vector<string> buildVector()
{
std::vector<string> local;
// .... build a vector
return std::move(local);
}
Run Code Online (Sandbox Code Playgroud)
我的理解是这是必需的.我经常看到std::unique_ptr从函数返回时使用的这个,但是GManNickG发表了以下评论:
我的理解是,在一个return语句中,所有局部变量都是自动xvalues(到期值)并将被移动,但我不确定它是否仅适用于返回的对象本身.所以OP应该继续把它放在那里,直到我更加自信它不应该是.:)
任何人都可以澄清是否std::move有必要吗?
行为编译器是否依赖?
通常从构造函数调用虚函数被认为是不好的做法,因为子对象中的被覆盖函数将不会被调用,因为尚未构造对象.
但是,请考虑以下类:
class base
{
public:
base() {}
~base() {}
private:
virtual void startFSM() = 0;
};
class derived final : public base
, public fsm_action_interface
{
public:
derived() : base{}
, theFSM_{}
{ startFSM(); }
/// FSM interface actions
private:
virtual void startFSM()
{ theFSM_.start(); }
private:
SomeFSMType theFSM_;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,类derived被标记为,final因此不存在其他子对象.因此,虚拟调用将正确解析(到最派生类型).
它仍然被认为是不好的做法吗?
在Andrei Alexandrescu关于错误处理的讨论中:
参见C++和2012年之后:Andrei Alexandrescu - C++中的系统错误处理(大约30分钟)
Andrei提供以下代码:
~Expected()
{
using std::exception_ptr;
if (gotHam) ham.~T();
else spam.~exception_ptr();
}
Run Code Online (Sandbox Code Playgroud)
这个析构函数正在清理一个union包含某种类型T或一个类型的析构函数std::exception_ptr.工会填充使用placement new.
然后安德烈解释说这using std::exception_ptr;是必要的,因为以下代码不解析:
else spam.~std::exception_ptr();
Run Code Online (Sandbox Code Playgroud)
这意味着如果需要在不同的命名空间中显式调用类的析构函数,则始终需要使用using指令.
为什么第二个例子不解析?
以下代码是否是有效的替代方案?
else delete spam;
Run Code Online (Sandbox Code Playgroud)
这是否与显式调用析构函数具有相同的效果 std::exception_ptr
如何将类型从可变参数模板参数转换为另一种类型?
例如:
template <typename... T>
struct single
{
std::tuple<T...> m_single;
};
template <typename... T>
struct sequences
{
single<T...> get(size_t pos)
{
// I don't know how to convert here
return std::make_tuple(std::get<0>(m_sequences)[pos]... std::get<N>(m_sequences)[pos]);
}
template <size_t Idx>
std::vector<
typename std::tuple_element<Idx, std::tuple<T...>>::type
>
get_sequence()
{
return std::get<Idx>(m_sequences);
}
std::tuple<T...> m_sequences; // std::tuple<std::vector<T...>> I don't know how to conver here
};
Run Code Online (Sandbox Code Playgroud)
我想这样写:
sequences<int, double, double> seq;
single<int, double, double> sin = seq.get(10);
Run Code Online (Sandbox Code Playgroud)
并且具有std::tuple<std::vector<int>, std::vector<double>, std::vector<double>>结构序列.并从中获得单身.
std::vector<single<T...>> 对我来说是个坏主意,因为我需要一个完整的序列,并且很容易从中复制它.
可能吗?
非常感谢你.对不起,我的英语不好.
Rvalue引用和Move语义是一个主要的C++ 11特性,它可以通过减少不必要的副本来显着加速代码.当使用c ++ 11/0x编译器时(例如gcc 4.6),STL已更新为使用此新功能
Boost 1.48引入了一个新的库,以便在旧的C++ 03编译器上模拟移动语义.这个库通过引入宏来扩展为真正的右值引用,当使用C++ 11编译器编译代码或使用C++ 03编译器编译代码时模拟rvalue引用.
除了boost::container更新任何其他的boost库以利用移动语义之外呢?
是否有一个路线图详细说明何时/如果将移动语义添加到其他boost库?
boost::multi_index_container已经提到在Boost.MultiIndex Future工作中添加了移动语义
,因为它是在1.31版本中引入的.
c++ ×10
c++11 ×9
boost ×2
boost-asio ×1
constructor ×1
doxygen ×1
dynamictype ×1
enums ×1
shared-ptr ×1
templates ×1
tuples ×1