我一直在使用一个可变参数模板,它在C和C++之间的接口中充当异常防火墙.模板只需要一个函数,然后是N个参数,并在try catch块中调用该函数.这一直很好,不幸的是我现在要调用的函数之一需要一个额外的默认参数.因此,函数名称未解析,模板无法编译.
错误是:
perfect-forward.cpp:在函数中
‘void FuncCaller(Func, Args&& ...) [with Func = void (*)(const std::basic_string<char>&, double, const std::vector<int>&), Args = {const char (&)[7], double}]’:
perfect-forward.cpp:69:41:从这里实例化
perfect-forward.cpp:46:4:错误:函数参数太少
代码的简化版本如下:
template< class Func, typename ...Args >
void FuncCaller( Func f, Args&&... params )
{
try
{
cout << __func__ << " called\n";
f(params...);
}
catch( std::exception& ex )
{
cout << "Caught exception: " << ex.what() << "\n";
}
}
void Callee( const string& arg1, double d, const vector<int>&v = vector<int>{} )
{ …Run Code Online (Sandbox Code Playgroud) Andrei Alexandrescu发表了一篇精彩的演讲,名为:Variadic模板是Funadic.
他提出了以下3个不同的扩展:
template <class... Ts> void fun( Ts... vs ) {
gun( A<Ts...>::hun(vs)...);
gun( A<Ts...>::hun(vs...));
gun( A<Ts>::hun(vs)...);
}
Run Code Online (Sandbox Code Playgroud)
他解释说:
调用1:
扩展全部Ts用于实例化class A,然后调用hun(vs)
然后在将它们传入时再次展开所有参数gun
召唤2:
展开所有Ts和所有vs单独
调用3:
锁定步骤中的Expnads,即:扩展参数1 Ts和vs
扩展参数2的Ts参数1和vs
扩展参数n Ts和参数n的参数2vs
关于可变参数模板的其他讨论似乎只涵盖了简单的可变参数类模板和可变参数函数,例如typesafe printf等.我不确定这些不同类型的扩展如何影响代码以及每种类型的用途.
有没有人举例说明每种扩展的应用?
我最近在我正在审查的一些代码中找到了以下函数定义:
void func( const::std::string& str )
{
// Do something...
}
Run Code Online (Sandbox Code Playgroud)
我很惊讶它const::std::string似乎是合法的(它汇编了GCC4.4,GCC 4.8,Clang 3.2和Intel 13.0.1).
标准是否指定const可以用作namespace?
我正在编写一些消息处理代码,其中每条消息都是POD结构.在写作的过程中,这将是定义一个抽象基类,每个消息类型都有虚拟函数,例如:
class AbstractHandler
{
public:
virtual void handleMessage( const MessageType1& msg ) =0;
virtual void handleMessage( const MessageType2& msg ) =0;
virtual void handleMessage( const MessageType3& msg ) =0;
virtual void handleMessage( const MessageType4& msg ) =0;
};
Run Code Online (Sandbox Code Playgroud)
然后创建实现处理函数的派生具体类:
class ConcreteHandler : public AbstractHandler
{
public:
virtual void handleMessage( const MessageType1& msg );
virtual void handleMessage( const MessageType2& msg );
virtual void handleMessage( const MessageType3& msg );
virtual void handleMessage( const MessageType4& msg );
};
Run Code Online (Sandbox Code Playgroud)
如果向系统添加新消息,则AbstractHandler必须与所有派生类型一起更新.
或者,我可以在mpl …
一个基本的C++ 03枚举类型只是一个带有花哨名称的整数值,因此我希望通过值传递它....
出于这个原因,我也希望boost::call_traits<T>::param_type通过T=SomeEnum确定最有效的传递方式T是按值.
从提升文档中可以看到呼叫特征:
定义一种类型,表示将类型T的参数传递给函数的"最佳"方式.
当我用boost::call_traits<T>::param_type用T=SomeEnum它确定SomeEnum应通过引用传递.
我也希望C++11 class enums也能通过价值传递.
测试代码:
#include <string>
#include <typeinfo>
#include <boost/call_traits.hpp>
#include <boost/type_traits/is_reference.hpp>
enum SomeEnum
{
EN1_ZERO = 0,
EN1_ONE,
EN1_TWO,
EN1_THREE
};
struct SomeStruct
{};
template<typename T>
void DisplayCallTraits( const std::string& desc )
{
typedef typename boost::call_traits<T>::param_type param_type;
std::cout << "-----------------------------------------------------\n";
std::cout << "Call traits for: " << desc << "\n";
std::cout << "\ttypeof T : " << typeid(T).name() << …Run Code Online (Sandbox Code Playgroud) 我所有的单元测试代码都基于boost::test. 我刚刚尝试了 GCC Address sanitizer,它报告了 boost::test 的一些问题:
==25309==ERROR: AddressSanitizer: heap-use-after-free on address 0xf5801344 at pc 0x8259412 bp 0xff9966c8 sp 0xff9966bc
READ of size 4 at 0xf5801344 thread T0
#0 0x8259411 in boost::unit_test::framework::run(unsigned long, bool) ../common/lib/boost/boost/test/impl/framework.ipp:450
#1 0x82732f7 in boost::unit_test::unit_test_main(bool (*)(), int, char**) ../common/lib/boost/boost/test/impl/unit_test_main.ipp:185
#2 0x827b5a3 in main ../common/lib/boost/boost/test/unit_test.hpp:59
#3 0x213ce5 in __libc_start_main (/lib/libc.so.6+0x16ce5)
#4 0x8238680 (/home/marpashl/lte/sw/build/x86/bin/framework_unit_test+0x8238680)
Run Code Online (Sandbox Code Playgroud)
我想隐藏此消息(因为它是针对测试库中的已知错误),以便我只能在自己的代码中看到问题。
有没有办法用 GCC 做到这一点?
注意编译器版本 GCC:/opt/gcc-x86-4.9.2/bin/c++
我发现可以使用 CLANG 文件将其列入黑名单,-fsanitize-blacklist=blacklist.txt但这目前不适用于 GCC。
我正在尝试使用GDB调试由许多共享库构建的应用程序。
gdb的开始:
prompt$ gdb
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-50.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Run Code Online (Sandbox Code Playgroud)
告诉GDB要调试的程序:
(gdb) file /home/me/build-path/my-program
Reading symbols from /home/me/build-path/my-program...done.
Run Code Online (Sandbox Code Playgroud)
在应用程序内设置一个断点: …
我正在尝试编写一个模板函数,它接受一个std::function取决于模板参数的函数.不幸的是,编译器无法正确地将参数deuced到std::function.这里有一些简单的示例代码
#include <iostream>
#include <functional>
using namespace std;
void DoSomething( unsigned ident, unsigned param )
{
cout << "DoSomething called, ident = " << ident << ", param = " << param << "\n";
}
template < typename Ident, typename Param >
void CallFunc( Ident ident, Param param, std::function< void ( Ident, Param ) > op )
{
op( ident, param );
}
int main()
{
unsigned id(1);
unsigned param(1);
// The following fails to compile …Run Code Online (Sandbox Code Playgroud) 如果类X派生自类Y,类Y具有以下任何一个:
移动构造函数和移动赋值运算符是否会为类X隐式默认,只要它声明以上都不是?
例如
struct Y
{
virtual ~Y() {}
// .... stuff
};
struct X : public Y
{
// ... stuff but no destructor,
// no copy/move assignment operator
// no copy/move constructor
// will X have a default move constructor / assignment operator?
};
Run Code Online (Sandbox Code Playgroud)
我目前正在使用gcc,但我主要对正确的行为应该是什么感兴趣(而不是特定的编译器是否符合标准).
我们的构建系统生成许多散布在源树周围的文件.
这些文件在运行时显示 svn status
在下面的示例中,所有文件都带有'?' 在thirdparty目录中已经由构建系统生成.
例如
svn status
M common/db/unit_test/src/db_payload_builder_test_suite.cpp
? common/db/unit_test/Makefile
M common/lib/osal/variant/linux/public_inc/osal_specific_msgq.hpp
M common/lib/enb/public_inc/enb_service.h
? thirdparty/lib/curl/public_inc_arm
? thirdparty/lib/curl/public_inc_x86
? thirdparty/lib/curl/originals/config.log
? thirdparty/lib/curl/originals/libcurl.pc
? thirdparty/lib/curl/originals/config.status
? thirdparty/lib/curl/originals/libtool
? thirdparty/lib/curl/originals/curl-config
? thirdparty/lib/curl/originals/src/curl
Run Code Online (Sandbox Code Playgroud)
我想告诉svn忽略这些文件.我不能简单地指定应该被忽略的目录,因为许多这些目录包含签入存储库的源.
上面的示例是生成的文件的子集,我们不能使用忽略模式,因为一些生成的文件与我们不希望忽略的文件匹配(例如xxx_.h")
我尝试过使用svn propset svn:ignore 'somefile' dir但我只能告诉svn忽略每个目录的一个特定文件.
如何为svn指定多个特定文件以忽略?