标签: c++03

POD与非POD类类型的默认初始化

C++标准说(8.5/5):

默认初始化类型对象T意味着:

  • 如果T是非POD类类型(第9节),T则调用默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的).

  • 如果T是数组类型,则每个元素都是默认初始化的.

  • 否则,该对象是零初始化的.

有了这段代码

struct Int { int i; };

int main()
{
    Int a;
}
Run Code Online (Sandbox Code Playgroud)

该对象a是默认初始化的,但显然a.i不一定等于0.这与标准是否相矛盾,IntPOD也不是数组?

编辑 已更改为class,struct因此这Int是一个POD.

c++ initialization c++03

4
推荐指数
1
解决办法
4162
查看次数

如何获取析构函数的成员函数指针?

假设我有

struct X {
  ~X() {}
};
Run Code Online (Sandbox Code Playgroud)

X::~X()在C++ 03中获取成员函数指针的类型是什么?

我不想实际调用它,只是在SFINAE中使用来确定是否存在给定类型的析构函数.

c++ destructor member-function-pointers c++03

4
推荐指数
1
解决办法
1981
查看次数

const_casting是一个可变字段安全吗?

考虑以下C++ 03程序:

#include <iostream>

struct T
{
    mutable int x;

    T() : x(0) {}
};

void bar(int& x)
{
   x = 42;
}

void foo(const T& t)
{
   bar(const_cast<int&>(t.x));
}

int main()
{
   T t;
   foo(t);
   std::cout << t.x << '\n';
}
Run Code Online (Sandbox Code Playgroud)

似乎有用,但肯定是安全的吗?

我只修改了一个mutable字段,但const完全剥离了它的上下文让我感到紧张.

c++ const c++03

4
推荐指数
1
解决办法
189
查看次数

如何设置全局容器(C++ 03)?

我想定义一个全局容器(C++ 03),这是我尝试过的一个示例代码,它不起作用.

#include <vector>
#include <string>
using namespace std;

vector<string> Aries;
Aries.push_back("Taurus");    // line 6

int main() {}
Run Code Online (Sandbox Code Playgroud)

编译错误:

prog.cpp:6:1: error: 'Aries' does not name a type
Run Code Online (Sandbox Code Playgroud)

我似乎可以定义一个空的全局向量,但无法填充它.看起来在C++ 03中,我也无法指定初始化器,例如:

vector<string> Aries = { "Taurus" };
Run Code Online (Sandbox Code Playgroud)

我在这里犯了错误,或者我如何解决这个问题?

我尝试在StackOverflow上搜索,看看之前是否已经回答了这个问题,但是只发现了这些帖子:C++中的全局对象,在C++定义全局常量,这对此没有帮助.

c++ containers global-variables c++03

4
推荐指数
3
解决办法
2416
查看次数

朋友类可以在C++ 03中有条件地声明吗?

我想仅在某些(编译时)条件为真时才声明友元类.例如:

// pseudo-C++
class Foo {
    if(some_compile_time_condition) {
        friend class Bar;
    }
};
Run Code Online (Sandbox Code Playgroud)

我没有在互联网上找到任何解决方案.我在编译时动态地完成了生成结构问题的所有答案.他们中的许多人使用C++ 11 std::conditional,但我想知道是否可以在不使用预处理器的情况下在C++ 03中执行此操作.

此解决方案/sf/answers/796369731/将无法工作,因为friend未继承船舶(具有继承的朋友类).

编辑只是为了使这更容易看到,如下面评论中所述:此要求不常见.这是我正在研究的硬件模拟新研究项目的一部分.测试平台是用C++编写的,我想在波形中显示变量.我已经研究了各种其他选项,并且friend class由于实际考虑,我发现我需要使用a .朋友将捕获值并生成波形,但我更愿意仅在需要波形时才拥有朋友,而不是所有时间.

c++ friend c++03

4
推荐指数
1
解决办法
576
查看次数

使用元编程计算非默认模板参数?

我有一个模板类,接受1到8个整数参数.每个参数的允许范围是0..15.每个参数的默认值16允许我检测未使用的参数.

我想将用户提供的参数数量作为编译时常量.我可以使用模板助手类和许多部分特化来完成此操作.

我的问题是,我可以使用一些递归元编程来清理它.我有什么作品,但感觉它可以在语法上得到改善.

遗憾的是,我无法使用变量模板和其他任何c ++ 0x.

#include <stdint.h>
#include <iostream>

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5,uint8_t p6,uint8_t p7>
struct Counter { enum { COUNT=8 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5,uint8_t p6>
struct Counter<p0,p1,p2,p3,p4,p5,p6,16> { enum { COUNT=7 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5>
struct Counter<p0,p1,p2,p3,p4,p5,16,16> { enum { COUNT=6 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4>
struct Counter<p0,p1,p2,p3,p4,16,16,16> { enum { COUNT=5 }; };

template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3>
struct Counter<p0,p1,p2,p3,16,16,16,16> { …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming c++03

4
推荐指数
1
解决办法
299
查看次数

为什么std :: setprecision(6)在固定宽度模式下流式传输超过六位数?

以下代码的输出:

#include <limits>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <sstream>

using namespace std;

inline string lexical_cast(const float arg)
{
    stringstream ss;
    ss << fixed << setprecision(numeric_limits<float>::digits10) << arg;
    if (!ss)
        throw "Conversion failed";

    return ss.str();
}

int main()
{
    cout << numeric_limits<float>::digits10 << '\n';
    cout << lexical_cast(32.123456789) << '\n';
}
Run Code Online (Sandbox Code Playgroud)

是:

6
32.123455

我期待,并希望:

6
32.1234

因为,就我所知,这就是float我能在系统上可靠地给予我的程度.

我怎样才能说服IOStreams按照我的意愿行事?

c++ iostream std c++03

4
推荐指数
1
解决办法
446
查看次数

控制台输出中出现意外的字符

我正在为Crysis Wars游戏编写一个新的服务器 - 客户端网络.我有一个函数将字符串居中到控制台窗口中每行支持的字符数.该窗口适合113个字符,但我已将函数中的最大字符宽度设置111为适合文本.

这是我的功能:

string Main::CenterText(string s)
{
    return string((111 - s.length()) / 2, ' ') + s; 
}
Run Code Online (Sandbox Code Playgroud)

这个功能来自我去年提出的一个问题,但我不确定我是否最终在过去的项目中使用它.

我试图在此上下文中使用此函数(该CryLogAlways函数只是将字符串记录到游戏/服务器日志文件并打印它):

CryLogAlways(CenterText("   ____     ____      _ __      _  _  __").c_str());
CryLogAlways(CenterText("  /  _/__  / _(_)__  (_) /___ _( )| |/_/").c_str());
CryLogAlways(CenterText(" _/ // _ \\/ _/ / _ \\/ / __/ // //_>  <  ").c_str());
CryLogAlways(CenterText("/___/_//_/_//_/_//_/_/\\__/\\_, / /_/|_|  ").c_str());
CryLogAlways(CenterText("                         /___/          ").c_str());
Run Code Online (Sandbox Code Playgroud)

但输出是:

在此输入图像描述

同样,@ deW1请求,我有一个类似的输出CryLogAlways(CenterText("X").c_str());:

在此输入图像描述

为什么我得到这个输出,我该如何解决这个问题?

c++ string c++03 crysis

4
推荐指数
1
解决办法
377
查看次数

std :: make_pair,c ++ 11和显式模板参数

Reedited:首先,这只是一个好奇的问题,我知道,std :: pair或许多其他解决方案可以根除这个问题.

你能告诉我,这个问题到底是什么原因?这段代码是一个简单的例子,用于c ++ 03,在c ++ 11上失败.

    std::pair<int*,int**> getsth(int* param)
    {
        return std::make_pair<int*,int**>(param, 0);
    }

    int main(int argc, char* argv[])
    {
        int* a = new int(1);
        std::pair<int*,int**> par = getsth(a);
        std::cout << *par.first;
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

我确实知道如何修复它以兼容这两个标准,但它让我感到厌恶,我不知道,在这种情况下,make_pair背后究竟是什么.

谢谢!

编辑:来自Coliru的编译错误消息:

main.cpp: In function 'std::pair<int*, int**> getsth(int*)':
main.cpp:8:47: error: no matching function for call to 'make_pair(int*&, int)'
     return std::make_pair<int*,int**>(param, 0);
                                               ^
main.cpp:8:47: note: candidate is:
In file included from /usr/local/include/c++/4.9.2/bits/stl_algobase.h:64:0,
                 from /usr/local/include/c++/4.9.2/bits/char_traits.h:39,
                 from /usr/local/include/c++/4.9.2/ios:40,
                 from /usr/local/include/c++/4.9.2/ostream:38,
                 from /usr/local/include/c++/4.9.2/iostream:39,
                 from main.cpp:1:
/usr/local/include/c++/4.9.2/bits/stl_pair.h:276:5: …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++03

4
推荐指数
1
解决办法
929
查看次数

引用using声明引入的函数的句子是什么意思?

我正在学习C++ 03标准,现在正在阅读[7.3.3]/11,但我无法理解以下段落:

如果命名空间范围或块范围中的函数声明与using声明引入的函数具有相同的名称和相同的参数类型,并且声明未声明相同的函数,则程序格式错误.

我在任何地方都没有找到任何这种情况的例子,我不明白这段经文的含义.

c++ c++03

4
推荐指数
1
解决办法
144
查看次数