标签: c++03

在 LLVM/Clang 下编译时,命名空间“std”中没有名为“unique_ptr”的类型

尝试unique_ptr在 Apple 平台上使用时遇到编译错误-std=c++11

$ make
c++ -std=c++11 -DNDEBUG -g2 -O3 -fPIC -march=native -Wall -Wextra -pipe -c 3way.cpp
In file included ...
./smartptr.h:23:27: error: no type named 'unique_ptr' in namespace 'std'
    using auto_ptr = std::unique_ptr<T>;
                     ~~~~~^
./smartptr.h:23:37: error: expected ';' after alias declaration
    using auto_ptr = std::unique_ptr<T>;
Run Code Online (Sandbox Code Playgroud)

根据 Marshall Clow 的说法,我认为他是Clang 和 AppleC++ 标准库专家:

技术报告 #1 (TR1) 是 C++03 标准的一组库添加。代表它们不是“官方”标准的一部分,它们被放置在命名空间 std::tr1 中。

在 c++11 中,它们正式成为标准的一部分,并存在于命名空间 std 中,就像向量和字符串一样。包含文件也不再位于“tr1”文件夹中。

带走:

  • Apple 和 C++03 = 使用 …

unique-ptr llvm-clang c-preprocessor c++11 c++03

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

Signed Char 到 Unsigned Int 转换的标准 C++ 行为

我快速浏览了 C++03 标准,但仍然无法判断这种行为是否得到保证:

\n\n
signed char cNegOne=-1; //char is 8bits\nunsigned int a=cNegOne; //int is 32 bits in my Windows system\nprintf("0x%x\\n",a);\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果是:

\n\n
0xffffffff\n
Run Code Online (Sandbox Code Playgroud)\n\n

VC++0xffffffff在 32 位 Windows 中提供。但我的假设是转换可能以两种方式发生:

\n\n

1) 8 位有符号 char -1 首先直接转换为 8 位无符号值,即二进制 11111111 或十进制 255,然后扩展为 32 位无符号 int,也给出 255 (0xff)。

\n\n

2) 8 位有符号 char -1 有符号扩展为 32 位有符号 int,给出 0xffffffff,然后重新解释为 32 位无符号 int。

\n\n

显然这里使用的是第二种方式。但为什么会这样呢?在标准中,我找不到任何谈论这个的内容。它是具体实施的吗?

\n\n

编辑:C++03 第 4 章的原文

\n\n

标准转换是为内置类型定义的隐式转换。第 4 条列举了完整的此类转换集。标准转换序列是按以下顺序进行的标准转换序列:

\n\n

\xe2\x80\x94 来自以下集合的零次或一次转换:左值到右值转换、数组到指针转换以及函数到指针转换。

\n\n

\xe2\x80\x94 …

c++ implicit-conversion c++03

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

C++:为std :: sort提供模板化比较函数

假设我想让std :: sort根据指针指向的int的值对指向int的指针进行排序.忽略那里明显的性能问题.简单吧?做一个功能:

bool sort_helper(const int *a, const int *b) 
{   
    return *a < *b;
}   
Run Code Online (Sandbox Code Playgroud)

并提供给std :: sort.

现在,如果我们也想用一个指向大对象的指针向量做同样的事情.同样的事情适用:首先我们<在对象中定义一个运算符,然后沿着以下行创建一个函数:

bool sort_helper(const ob_type *a, const ob_type *b) 
{   
    return *a < *b;
}   
Run Code Online (Sandbox Code Playgroud)

或者其他什么,将它提供给std :: sort.

现在,这就是它变得棘手的地方:如果我们想要将任何类型的指针向量排序,使用任意比较函数(我们假设我们使用该函数的任何类型,将能够使用它) ) - 提供上面sort_helper函数的模板版本很简单:

template <class ob_type>
bool sort_helper(const ob_type *a, const ob_type *b) 
{   
    return *a < *b;
}   
Run Code Online (Sandbox Code Playgroud)

但是,提供任意比较功能更难:这样的事情 -

template <typename comparison_function, class ob_type>
bool sort_template_helper(const ob_type *a, const ob_type *b)
{
    return comparison_function(*a, *b);
}

template <typename comparison_function, …
Run Code Online (Sandbox Code Playgroud)

c++ sorting pointers c++03

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

`std::find()` 是否短路?

考虑我有一个std::vector这样的,它的内容是std::strings 和{"a","b","c"}。如果我要执行std::find()寻找"a",它会在迭代结束后停止"a"(即短路)还是继续到最后?

std::vector<std::string> vec;
vec.insert(vec.begin(), "a");
vec.insert(vec.begin(), "b");
vec.insert(vec.begin(), "c");
Run Code Online (Sandbox Code Playgroud)

哪个更快?

std::find(vec.begin(), vec.end(), "a");
std::find(vec.begin(), vec.end(), "c");
Run Code Online (Sandbox Code Playgroud)

c++ iterator vector c++03

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

为什么C++ 03文件流不接受字符串构造函数参数?

为什么以下代码编译C++11而不在C++03(两个gcccl)

#include <string>
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) {
    const std::string t("Hello");
    std::ofstream out(t);
}
Run Code Online (Sandbox Code Playgroud)

为什么C++03流不接受std::string构造函数参数?这个决定是基于某些事情还是偶然发生的?

c++ fstream library-design c++11 c++03

3
推荐指数
2
解决办法
256
查看次数

确保派生类构造函数必须调用特定的基类方法

在C++(03)类中,我有一个成员变量,在对象构造期间必须为其赋值.但是,只有派生类才能计算所需的值.正如本文所讨论的,C++是否要求您从派生类初始化基类成员?,我知道派生类不能初始化基类成员,但是赋值对我来说已经足够了.

因此,我在基类中提供了一个赋值的方法,但我无法弄清楚如何强制派生类调用它.以下示例代码说明了这一点.

class Base {
public:
    Base() {}
    setFoo(unsigned inFoo) { foo = inFoo; }
private:
    unsigned foo;
};

class Derived : public Base {
    Derived() : Base() { 
        unsigned desired = ... // do calculations to get the desired value
        setFoo(desired);  // --> how to ensure that derived class calls this?
    }
};
Run Code Online (Sandbox Code Playgroud)

foo随后其他几个基类的方法使用.即使我在其中设置了"标志" setFoo(),也没有"后构造函数"方法,我可以检查它.在基类中还有其他几种方法foo可供使用.我可以在那里检查它,但它太繁琐,容易出错并且效率低下.

如果以前曾经问过我,我很抱歉.在尝试通过搜索"确保派生类构造函数必须调用特定基类方法"和"强制派生类来分配基类成员"之前,我确实尝试在此处找到它.我遇到了一些有用的帖子如何在C++的派生类'构造函数中初始化基类的const变量?强制派生类使用基类的构造函数,但遗憾的是它们没有解决我的问题.

你能否建议我采取适当的方法来解决这种情况?编译时检查解决方案是可取的,但如果不可行,运行时检查也会有所帮助.虽然我不能将C++ 11用于我的问题,但我也很乐意学习任何C++ 11解决方案.谢谢你的考虑.

编辑当然,将此作为一项要求进行记录也是一种选择,但我想了解是否有程序化解决方案.

Edit2 …

c++ inheritance c++03

3
推荐指数
1
解决办法
1915
查看次数

std :: string的默认构造函数是否为throw?

可以std :: string s; 在任何情况下扔?这是否受标准规范(对C++ 03感兴趣,如果存在差异)?

c++ stdstring default-constructor c++03 nothrow

3
推荐指数
1
解决办法
2224
查看次数

使用SFINAE检测是否有(增强)范围

对于日志代码,我想检测模板函数的给定参数是否可以使用Boost.Range中的工具进行迭代.显然我需要实例化不同的代码,不管是不是,所以我需要SFINAE,可能(当然)与boost :: enable_if结合使用.我试着检测是否beginendfree函数的定义,就像这样:

namespace is_range_impl {
    template <typename T> T &make();
    struct any { template <class T> any(T const&); };
    struct not_range {};
    not_range begin(const any &);
    not_range end(const any &);
    struct no_type { char x[8]; };
    typedef char yes_type;
    template <typename T> yes_type check(const T &t);
    no_type check(const not_range &t);
    using boost::begin;
    using boost::end;
    template <typename T> struct is_range_impl {
        enum e {
            value = (sizeof(check(begin(make<T>()))) == sizeof(yes_type) &&
                     sizeof(check(end(make<T>()))) == sizeof(yes_type)),
        }; …
Run Code Online (Sandbox Code Playgroud)

c++ boost sfinae c++03

3
推荐指数
1
解决办法
784
查看次数

二维矢量打印

我有一个二维字符串向量,我需要打印出来.整个程序应从txt文件中读取一行,将每个单词作为不同的元素存储,然后将"单词向量"推入包含例如100行的向量中.我已经把一切都搞定了,但是当我必须打印矢量时问题出现了.每行可以有不同数量的单词,例如:

我喜欢蛋糕

很多.

所以我不能用:

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        cout << vec[i][j];
    }
}
Run Code Online (Sandbox Code Playgroud)

因为第二行不包含3个元素,程序关闭.
知道怎么做吗?注意:我的讲师不接受C++ 11,因此可以理解基于C++ 98的解决方案.这是我的功能:

void readline(vector<vector<string> >& lines, int size)
{
    vector<string> row;
    string line, word;
    fstream file;
    istringstream iss;
    int i;

    file.open("ticvol1.txt", ios::in);
    for (i = 0; i < size; i++)
    {
        getline(file, line);
        iss.str(line);
        while (iss >> word) row.push_back(word);
        lines.push_back(row);
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ vector c++03

3
推荐指数
2
解决办法
3万
查看次数

C++使用较新编译器的功能生成供旧编译器使用的代码

我一直在研究"新的"C++标准(C++ 11和C++ 14)的一些特性,这让我想到了一些东西.我目前正在为我的项目使用VC++ 2008编译器(出于各种原因),这意味着我可以访问的最新标准是C++ 03和TR1.TR1有一些不错的东西,但C++ 11和C++ 14中的功能很不错.

我的问题是:我是否有任何方法可以使用较新的编译器(比如MSVC2012或2013)使用较新的C++ 11和C++ 14功能构建库或DLL,然后将其链接到我的正在运行'08编译器的项目?

我能想到的唯一不起作用的是我必须在我的'08编译器项目包含的头中拥有C++ 11或C++ 14特性的任何地方.但是,只要所有"新"都隐藏在我的界面后面,这不应该起作用吗?

c++ visual-c++ c++11 c++03 c++14

3
推荐指数
1
解决办法
184
查看次数