小编Gra*_*eme的帖子

对于非内置类型,使用const值返回函数有什么用例?

最近我读过,从函数返回值来为非内置类型限定返回类型const是有意义的,例如:

const Result operation() {
    //..do something..
    return Result(..);
}
Run Code Online (Sandbox Code Playgroud)

我很难理解这个的好处,一旦对象被返回肯定是调用者的选择来决定返回的对象是否应该是const?

c++ const-correctness

40
推荐指数
4
解决办法
7710
查看次数

虚拟继承对性能的影响

我正在考虑在实时应用程序中使用虚拟继承.使用虚拟继承是否会产生类似于调用虚函数的性能影响?有问题的对象只会在启动时创建,但我担心层次结构中的所有函数是否都将通过vtable调度,或者只是来自虚拟基类的函数.

c++ performance real-time virtual-inheritance

26
推荐指数
1
解决办法
7539
查看次数

在C++ 11中抛出时,异常是否使用移动语义?

http://www.drdobbs.com/cpp/practical-c-error-handling-in-hybrid-env/197003350?pgno=4

在本文中,Herb Sutter解释说,抛出异常需要异常的副本,因为它是作为临时创建的,因此使用a std::auto_ptr来绕过副本开销.根据C++ 11中提供的移动语义,这仍然是必要的吗?

c++ move-semantics c++11

18
推荐指数
1
解决办法
1726
查看次数

从专用模板类函数调用非专用模板类函数

是否可以从专用模板类中调用非专用模板类中定义的函数?这是我正在尝试的一个例子:

template <typename T>
struct Convert
{
 static inline void toString(unsigned num, unsigned places, std::string& str) { ... }
};

template <>
struct Convert<int8_t>
{
 static inline void toString(unsigned num, std::string& str)
 {
   Convert<int8_t>::toString(num, digitis(num), str);
 }
};
Run Code Online (Sandbox Code Playgroud)

海湾合作委员会抱怨它看不到非专业化的阶级职能; 也就是说,我猜它只在专门的班级内看.

有什么想法吗?

编辑

以下是我的代码中的一个更具体的示例(可能的解决方案):

struct NonSpecial { };

template <typename T>
class Convert
{

        template <typename R>
        static inline R fromString(const register char *str, const unsigned str_len)
        {   
            R result = 0;
            //convert str to R
            return result;
        }

        friend class …
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization

16
推荐指数
1
解决办法
2364
查看次数

clang 3.1看不到unique_ptr?

我刚开始玩clang并试图编译以下示例程序:

#include <memory>
#include <iostream>

int main()
{
    std::unique_ptr<unsigned> u(new unsigned(10));
    std::cout << *u << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我得到以下错误:

$ clang++ helloworld.cpp 
helloworld.cpp:6:10: error: no member named 'unique_ptr' in namespace 'std'
    std::unique_ptr<unsigned> u(new unsigned(10));
    ~~~~~^
helloworld.cpp:6:29: error: expected '(' for function-style cast or type construction
    std::unique_ptr<unsigned> u(new unsigned(10));
                    ~~~~~~~~^
helloworld.cpp:6:31: error: use of undeclared identifier 'u'
    std::unique_ptr<unsigned> u(new unsigned(10));
                              ^
helloworld.cpp:7:19: error: use of undeclared identifier 'u'
    std::cout << *u << std::endl;
                  ^
4 errors generated.
Run Code Online (Sandbox Code Playgroud)

我在Mac OS X上使用Clang …

c++ clang c++11

16
推荐指数
1
解决办法
8271
查看次数

INT_ [MIN | MAX]限制宏与numeric_limits <T>

是否有任何关于使用数字限制宏(例如INT64_MAX)而不是std :: numeric_limits的论据?根据我的理解,numeric_limits在标准中,但宏仅在C99中,因此非标准.

c++

12
推荐指数
4
解决办法
3451
查看次数

链接使用不同版本的GCC构建的目标文件

我对使用现代版本的GCC 4.4.x/4.5.x构建C++库有一些兼容性问题,其中旧版本的客户端,例如3.4.x/4.1.x. 已经提出的一个解决方案是编译目标文件并分发它们.然后,客户可以使用任何版本的GCC和相关的ABI进行链接.一些问题:

  1. 它是否正确?
  2. 我应该静态链接以避免libstdc ++兼容性问题吗?
  3. 这是不必要的(我听说gcc 3.4以后是向前兼容的)?

干杯,格雷姆

c++ compiler-construction gcc

10
推荐指数
1
解决办法
3645
查看次数

模拟函数与签名中的类模板参数

是否可以使用gmock来模拟一个包含类模板参数的函数?例如:

template <typename T>
struct Mockable
{
    virtual void do_work(const int num, const T& value) = 0;
};

template <typename T>
struct MockMockable : Mockable<T>
{
    MOCK_METHOD2(do_work, void(const int, const T&));
};
Run Code Online (Sandbox Code Playgroud)

c++ testing mocking gmock

9
推荐指数
1
解决办法
5468
查看次数

C++库兼容性

我目前正在编写一个库,正在考虑从GCC 4.1.2升级到GCC的4.5.2(最新版本).如果我将我的代码编译成静态库,我可以假设编译器兼容性(显然在相同的操作系统上)对于客户端应该是非问题吗?

编辑 为了进一步澄清:如果我向客户端提供一个使用gcc 4.5.2编译的静态链接库,那么这个库的用户在编译器和必须使用的版本方面有什么限制?

c++ compiler-construction gcc binary-compatibility

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

为Python记录器格式化输出编写单元测试

你怎么能用Python编写一个单元测试来测试记录器的输出确实是你期望的格式(即通过调用logging.basicConfig()来设置)?我正在考虑自定义StreamHandler并使用're'库,但它看起来不像传递给StreamHandler.emit()的LogRecord可以给我输出的字符串.

python logging unit-testing

7
推荐指数
1
解决办法
2845
查看次数