标签: c++03

为什么使用=来初始化C++中的原始类型?

在我工作的地方,人们通常认为对象最好使用C++样式构造(带括号)初始化,而原始类型应该用=运算符初始化:

std::string strFoo( "Foo" );
int nBar = 5;
Run Code Online (Sandbox Code Playgroud)

但是,似乎没有人能够用这种方式解释他们为什么喜欢这样的东西.我可以看到std::string = "Foo";效率低下,因为它会涉及额外的副本,但是=完全放弃运算符并在各处使用括号有什么不对?

这是一个共同的惯例吗?这背后的想法是什么?

c++ coding-style c++03

8
推荐指数
3
解决办法
2801
查看次数

如何定义nullptr以支持C++ 03和C++ 11?

可能重复:
"backporting"nullptr到C++ - pre-C++ 0x程序

如何定义nullptr支持C++ 03和C++ 11?

下面的代码是用C++ 03和C++ 11编译编译的,而不改变C++ 11编译器中nullptr的含义吗?

#include <cstddef>

#if !defined(nullptr)
#define nullptr NULL
#endif
Run Code Online (Sandbox Code Playgroud)

c++ nullptr c++11 c++03

8
推荐指数
2
解决办法
9584
查看次数

C++ 03下的参考折叠

我需要从绑定成员函数创建一个谓词,所以我将它包装在一个boost::function<bool(SomeObject const &)>.这看起来很好,但是我还需要在一个案例中否定它.然而

boost::function<bool(SomeObject const &)> pred;
std::not1(pred);
Run Code Online (Sandbox Code Playgroud)

不能在MSVC++ 9.0(Visual Studio 2008)下编译,抱怨对引用的引用无效:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\functional(213) : warning C4181: qualifier applied to reference type; ignored
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\functional(213) : error C2529: '_Left' : reference to reference is illegal
Run Code Online (Sandbox Code Playgroud)

问题是boost::function定义argument_typeas SomeObject const &std::unary_negate<_Fn1>实例化由std::not1内部尝试使用const typename _Fn1::argument_type&和编译器拒绝它因为T::argument_type已经是一个引用.我确信这应该在C++ 11下编译,但这只是旧的编译器,只是C++ 03.所以我想知道它是谁的错:

  • 编译器,因为它应该折叠引用(显然不是),
  • 标准库,因为它应该准备好处理引用的函子(显然不是,因为规范定义unary_negateconst typename Predicate::argument_type& x参数),
  • 提升,因为argument_type即使实际参数是或者,也不应该引用 …

visual-c++-2008 c++03

8
推荐指数
1
解决办法
358
查看次数

是否有可能在编译时获得包含命名空间和类名的字符串?

我不知道如何界定,将在格式定的类名称输出其命名空间和类名像宏:"Namespace.SubNamespace.ClassName"?

所以写这样的东西:

// MyClass.h
#include <string>

namespace NS {
 namespace SNS {
  class MyClass {
    static std::string str;
  };
 }
}

//MyClass.cpp
#include <MyClass.h>
using namespace std;
string NS::SNS::MyClass::str = SUPER_MACRO(/*params if needed yet none would be prefered*/);
Run Code Online (Sandbox Code Playgroud)

我想让str成为"NS.SNS.MyClass".如果可能的话,我希望那个宏有fiew params(意思是一个或没有).

或者我想知道是否可以使用以下类似的模板来完成这样的事情:

string NS::SNS::MyClass::str = GetTypeNameFormater<NS::SNS::MyClass>();
Run Code Online (Sandbox Code Playgroud)

怎么做这样的事情(使用boost,stl并且手头只有C++ 03)?

c++ boost class c++03

8
推荐指数
1
解决办法
734
查看次数

has_trivial_default_constructor如何工作?

有人可以解释一下我的has_trivial_default_constructor工作原理吗?我尝试在boost实现中找到它但不幸的是有太多的宏而我只是迷路了...

有人可以trivial_default_constructor使用模板检测C++中的内容吗?

我需要一个C++ 03而不是11的例子.

#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

  struct A{
    A(){}
    int a;
    //std::vector< int > b;
  };

int main(int argc, char* argv[])
{
  struct B{
    std::vector< int > b;
  };


  bool result =  boost::has_trivial_default_constructor<A>::value;

  //std::forward(&test);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ metaprogramming c++03

8
推荐指数
1
解决办法
522
查看次数

构造函数是否需要使用显式关键字来获取多个参数?

这个问题与前面的C++ 11标准(C++ 03)有关.explicit防止从一种类型到另一种类型的隐式转换.例如:

struct Foo
{
    explicit Foo(int);
};

Foo f = 5; // will not compile
Foo b = Foo(5); // works
Run Code Online (Sandbox Code Playgroud)

如果我们有一个带有两个或更多参数的构造函数,会explicit阻止什么?我知道在C++ 11中你已经进行了初始化,因此它会阻止构造,例如:

struct Foo
{
    explicit Foo(int, int);
};

Foo f = {4, 2}; // error!
Run Code Online (Sandbox Code Playgroud)

但是在C++ 03中我们没有支持初始化,所以explicit关键字阻止了什么类型的构造呢?

c++ c++03

8
推荐指数
2
解决办法
132
查看次数

使用std :: ptr_fun作为成员函数

考虑以下:

class A
{
    public:
    bool is_odd(int i)
    {
        return (i % 2) != 0;
    }

    void fun()
    {
        std::vector<int> v2;
        v2.push_back(4);
        v2.push_back(5);
        v2.push_back(6);

        // fails here
        v2.erase(std::remove_if(v2.begin(), v2.end(), std::not1(std::ptr_fun(is_odd))), v2.end());
    }
};
Run Code Online (Sandbox Code Playgroud)

上面的代码无法否定is_odd()因为它是成员函数的影响.呼叫std::ptr_fun()失败.

我该如何使它工作?请注意,我想is_odd()成为非静态成员函数.

c++ c++03

8
推荐指数
1
解决办法
831
查看次数

C++ 98/03 std :: is_constructible实现

我的业余爱好库的基本组件必须与C++ 98和C++ 11编译器一起使用.要了解和欣赏自己,我创建了几个类型支持的功能(如C++ 98级的实现enable_if,conditional,is_same,is_integral为了使用它们的时候没有C++ 11的支持等...).

然而,当我实施时,is_constructible我陷入困境.有没有任何模板魔术(某种SFINAE),我可以用它来实现它而不需要C++ 11支持(declval)?

当然在C++ 03中没有可变参数模板支持,所以我将把实现专门化到一定深度.主要问题是,是否存在可以决定T是否可以从给定类型构造的技术.

c++ compile-time sfinae c++11 c++03

8
推荐指数
3
解决办法
1464
查看次数

为numeric_limit <T> :: max()指定无限制的限制?

我有一个任意的精度Integer类,它很像Java BigInteger或OpenSSL BIGNUM在运行.我无法理解我应该如何表达无限制的限制numeric_limit<Integer>::max().

Stack Overflow有几个问题,询问它是否可以执行(比如可以将std :: numeric_limits专门用于用户定义的类似数字的类吗?),以及一些使用基元的示例的答案,但我没有看到使用任意精度整数类的示例.我还访问了std :: numeric_limits参考页面,但我不清楚在这种情况下我应该做些什么.

在这一点上,我的内容是可以专注numeric_limit于我的Integer,并且可以将它放在标准命名空间中.我还需要专注于所有人numeric_limits.

如何指定无限制的限制numeric_limit<T>::max()


以下是GCC 4.2.1 <limits>(OS X机器).

/// numeric_limits<int> specialization.
template<>
  struct numeric_limits<int>
  {
    static const bool is_specialized = true;

    static int min() throw()
    { return -__INT_MAX__ - 1; }
    static int max() throw()
    { return __INT_MAX__; }

    static const int digits = __glibcxx_digits (int);
    static const int digits10 = __glibcxx_digits10 …
Run Code Online (Sandbox Code Playgroud)

c++ numeric-limits c++03

8
推荐指数
1
解决办法
161
查看次数

这是在C++ 03中执行"Expression SFINAE"的有效方法吗?

在C++ 11中,SFINAE很容易判断表达式是否有效.举个例子,假设检查某些东西是否可流动:

template <typename T>
auto print_if_possible(std::ostream& os, const T& x) 
    -> decltype(os << x, void());
Run Code Online (Sandbox Code Playgroud)

print_if_possible将只参加重载解析如果os << x是合式表达.

godbolt.org上的实例


我需要在C++ 03中做同样的事情,我发现这sizeof可能有所帮助(因为我需要一个表达式的未评估上下文).这就是我想出的:

template <int> struct sfinaer { };

template <typename T>
void print_if_possible(std::ostream& os, const T& x, 
    sfinaer<sizeof(os << x)>* = NULL);
Run Code Online (Sandbox Code Playgroud)

godbolt.org上的实例


似乎g ++clang ++的最新版本都接受了这个sizeof版本-std=c++03 -Wall -Wextra.

  • 代码是否保证在C++ 03中按预期工作?

  • 它是正确的结论是C++ 11的表达SFINAE的任何使用可回迁C++ 03使用到sfinaersizeof

c++ sizeof decltype sfinae c++03

8
推荐指数
1
解决办法
150
查看次数