我知道在C++ 11之前,auto关键字具有完全不同的含义; 它是一个存储类型说明符,指示具有自动存储类型的对象(即放置在堆栈上).
理论就是这样......你将如何实际使用这个关键字(语法),为什么?另外,我还没有在实际代码中看到这个关键字pre-C++ 11; 什么时候有用(什么时间段)?
(注意: 正如标签中已经清楚的那样,这是严格的 C++ 03. 是的,我知道,lambda会让所有这些痛苦消失(带来新的种类,我打赌),但这是一个嵌入式系统,有一个90年代的操作系统版本,我被告知我应该很高兴我有一个C++ 03编译器(GCC4.1.x,BTW)或C++编译器.所以请不要发布C++ 11解决方案,无需擦它,真的.
另外,std::bind(),std::function()等等,当然,实际std::tr1的,但我编辑了的 tr1前缀,因为我认为这增加了大多只有噪声的代码.)
我有一些类似服务器的东西,我需要注册函数,我需要调整它们来调用一些对象类似但略有不同的函数.这些函数具有不同的参数列表.服务器"知道"当我尝试注册一个函数时,它只接受一个具有正确签名的函数(根据std::function需要正确),这取决于作为模板参数传入的一些魔术标记.
这是代码的草图:
// this I just use
class server {
public:
template<unsigned int MagicTag>
bool register_call(typename some_traits_type<MagicTag>::func_type);
};
// this needs to be called from the server
class X {
public:
bool foo();
bool bar(std::string&);
bool baz(int);
};
// this is the glue
class Y {
public:
Y(X& x) : x_(x) {
register_call<MAGIC_FOO>(&Y::foo );
register_call<MAGIC_BAZ>(&Y::bar, _1);
register_call<MAGIC_FBZ>(&Y::baz, _1); …Run Code Online (Sandbox Code Playgroud) 卡在TR1中,对于测试程序,我需要对特定类型的多个对象执行某些操作.我有几个元组类型定义,如下所示:
typedef std::tr1::tuple< bool
, signed char
, signed short
, signed int
, signed long long
, unsigned char
, unsigned short
, unsigned int
, unsigned long long > integral_types;
Run Code Online (Sandbox Code Playgroud)
从每个元组类型创建一个对象.然后我有类似这样的功能模板:
template<typename T>
void invoke_operation_1(T& obj);
Run Code Online (Sandbox Code Playgroud)
需要为元组对象中的所有对象调用它们.
我如何在C++ 03中做到这一点?
我写了一个与C++ 03兼容的实现的尝试is_default_constructible:
template<class = void> struct is_default_constructible;
template<> struct is_default_constructible<>
{
protected:
// Put base typedefs here to avoid pollution
struct twoc { char a, b; };
template<bool> struct test { typedef char type; };
template<class T> static T declval();
};
template<> struct is_default_constructible<>::test<true> { typedef twoc type; };
template<class T> struct is_default_constructible : is_default_constructible<>
{
private:
template<class U> static typename test<!!sizeof(::new U())>::type sfinae(U*);
template<class U> static char sfinae(...);
public:
static bool const value = sizeof(sfinae<T>(0)) > 1; …Run Code Online (Sandbox Code Playgroud) 我能找到的所有文档都说std::basic_istream<>::ignore(n)"提取并丢弃字符",但这种提取意味着什么并不十分清楚.
对于std::ifstream特别,可以实现使其等同于简单地seekg在文件中-ing?如果是这样,主流实现会这样做吗?
否则,如果字符在被丢弃之前确实被"读取",那么ignore在seekg可用时似乎是一个糟糕的选择(例如,使用文件或字符串流).
从c ++ 11开始,我们可以这样做:
template<class A, class B> class C{};
template<class A> using D = C<A, int>;
Run Code Online (Sandbox Code Playgroud)
所以D是C有B=int.
typedef在c ++ 03中有没有办法做到这一点?
这不起作用:
template <class A> typedef C<A, int> D;
Run Code Online (Sandbox Code Playgroud) C++ 03标准§23.2.4.3/3 std::vector::erase(iterator position)具体描述和说明
在擦除点之后使所有迭代器和引用无效.
是Iterator 在擦除点不失效?具体来说,如果我有一个vector单独的元素,我将begin()迭代器复制到局部变量然后调用
vec.erase(vec.begin())
Run Code Online (Sandbox Code Playgroud)
我在局部变量中的迭代器是否会失效?
在擦除点之后或者包括擦除点之后,迭代器是否会失效?
我试图通过使用异常catch-rethrows来调试我的应用程序.我的异常处理代码比我正在调试的一些块长,并且它都是复制粘贴的.
有没有更好的方法来反复表达以下代码?我怀疑宏是这里的方式,但我通常会避免像瘟疫这样的宏.
try {
// Code here...
}
catch (std::exception & e)
{
ErrorMsgLog::Log("Error", "std exception caught in " __func__ " " __FILE__ " " __LINE__, e.what());
throw e;
}
catch (Exception & e)
{
ErrorMsgLog::Log("Error", "Builder exception caught in " __func__ " " __FILE__ " " __LINE__, e.Message);
throw e;
}
catch (...)
{
ErrorMsgLog::Log("Error", "Unknown exception caught in " __func__ " " __FILE__ " " __LINE__);
throw std::runtime_error ("Unknown Exception in " __func__ " " __FILE__ " " …Run Code Online (Sandbox Code Playgroud) 这似乎与包含常量成员的POD结构类似,但有点相反.
#include <iostream>
struct A
{
int a;
};
union U
{
volatile A a;
long b;
};
int main()
{
U u1;
U u2;
u1.a.a = 12;
u2 = u1;
std::cout << u2.a.a << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++ 4.8.3编译此代码时没有错误,并且运行正常:
$ g++ -std=c++03 a.cpp -o a_gcc
$ ./a_gcc
12
Run Code Online (Sandbox Code Playgroud)
但是clang ++ 3.5.1会产生错误(我手动包装错误消息以防止代码框滚动):
$ clang++ -std=c++03 a.cpp -o a_clang
a.cpp:8:7: error: member function 'operator=' not viable: 'this'
argument has type 'volatile A', but function is not marked …Run Code Online (Sandbox Code Playgroud) 举个例子:
#include <iostream>
class A
{
public:
static const int numberOfWheels = 4;
};
// const int A::numberOfWheels;
int main()
{
std::cout << A::numberOfWheels << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
难道是正式未定义行为(UB),因为A::numberOfWheels是使用没有它的定义是什么?(另见这里).正如C++ 03所述:
如果在程序中使用该成员,并且名称空间范围定义不包含初始化程序,则该成员仍应在名称空间作用域中定义.
我发现在C++ 03 中使用的定义相当混乱,因为它指向可能被评估的表达式:
如果对象或非重载函数的名称出现在可能已评估的表达式中,则使用该函数.
从我的猜测来看,它排除了以下表达式:
sizeof(A::numberOfWheels) ;
typeid(A::numberOfWheels).name() ;
Run Code Online (Sandbox Code Playgroud)
但不一定是<<像上面那样重载运算符的表达式.