小编Mar*_*dik的帖子

如何在C++中创建条件typedef

我想做这样的事情:

#include <iostream>
#include <random>

typedef int Integer;

#if sizeof(Integer) <= 4
    typedef std::mt19937     Engine;
#else
    typedef std::mt19937_64  Engine;
#endif

int main()
{
    std::cout << sizeof(Integer) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

error: missing binary operator before token "("
Run Code Online (Sandbox Code Playgroud)

我怎样才能正确地创建条件typedef?

c++ c++11

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

为什么派生类不能在此代码中调用受保护的成员函数?

#include <iostream>

class Base
{  
protected:
    void somethingProtected()
    {
        std::cout << "lala" << std::endl;
    }
};

class Derived : public Base
{
public:
    void somethingDerived()
    {
        Base b;
        b.somethingProtected();    // This does not compile
        somethingProtected();      // But this is fine
    }
};

int main()
{
    Derived d;
    d.somethingDerived();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想也许只有受保护的成员this可以使用,其他实例的受保护成员永远无法访问.

但:

class Derived : public Base
{
public:

    void somethingDerived(Derived& d)
    {
        d.somethingProtected();  // This compiles even though d is
                                 // potentially a different instance
    } …
Run Code Online (Sandbox Code Playgroud)

c++ oop

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

C++中静态多态性背后的动机是什么?

我使用Curiously Recurring模板模式了解静态多态的机制.我只是不明白它有什么好处.

声明的动机是:

我们牺牲了动态多态性的一些灵活性来提高速度.

但是,为什么东西打扰这么复杂,如:

template <class Derived>
class Base
{
public:
    void interface()
    {
         // ...
         static_cast<Derived*>(this)->implementation();
         // ...
    }
};

class Derived : Base<Derived>
{
private:
     void implementation();
};
Run Code Online (Sandbox Code Playgroud)

当你可以做的时候:

class Base
{
public: 
    void interface();
}

class Derived : public Base
{
public: 
    void interface();
}
Run Code Online (Sandbox Code Playgroud)

我最好的猜测是代码中没有语义差异,这只是一个好的C++风格问题.

Herb Sutter写道Exceptional C++ style: Chapter 18:

更喜欢将虚拟功能设为私有.

当然伴随着彻底解释为什么这是好风格.

在本指南的上下文中,第一个例子是好的,因为:

void implementation()示例中的函数可以假装是虚拟的,因为它是在这里执行类的自定义.因此它应该是私人的.

第二个例子很糟糕,因为:

我们不应该干涉公共接口来执行自定义. …

c++ coding-style

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

如何正确使用std :: reference_wrappers

我想了解std::reference_wrapper.

以下代码显示引用包装器的行为与引用完全不同.

#include <iostream>
#include <vector>
#include <functional>

int main()
{
    std::vector<int> numbers = {1, 3, 0, -8, 5, 3, 1};

    auto referenceWrapper = std::ref(numbers);
    std::vector<int>& reference = numbers;

    std::cout << reference[3]              << std::endl;
    std::cout << referenceWrapper.get()[3] << std::endl; 
              // I need to use get ^
              // otherwise does not compile.
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,隐式转换不适用于调用成员函数.这是一个固有的限制吗?我需要std::reference_wrapper::get经常使用吗?

另一个案例是这样的:

#include <iostream>
#include <functional>

int main()
{
    int a = 3;
    int b = 4;
    auto refa = std::ref(a);
    auto …
Run Code Online (Sandbox Code Playgroud)

c++ stl c++-standard-library c++11

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

我可以通过引用强制C++ 11 lambda返回吗?

由于lambda表达式返回值,因此无法编译:

#include <iostream>

class Item
{
public:
    int& f(){return data_;}
private:
    int data_ = 0;
};

int main()
{
    Item item;
    auto lambda = [](Item& item){return item.f();};
    lambda(item) = 42;  // lambda(item) is a rvalue => compile time error 
    std::cout << item.f() << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有没有解决的办法?我可以强制lambda 通过引用返回吗?

c++ lambda c++11

29
推荐指数
1
解决办法
5700
查看次数

为什么不总是包括所有标准标题?

我正在阅读Herb Sutter More Exceptional C++关于前瞻性声明的第37项说:

#include当前瞻声明足够时,永远不要头.宁愿#include<iosfwd>当不需要流的完整定义.

另外,我听到很多关于仅包含编译单元所需的头以减少依赖性的建议.

我完全理解为什么这应该适用于项目标题,但我不太明白为什么包含不必要的标准标题是不好的.

例如,我做这样的事情:

//standard_library.h

#ifndef STANDARD_LIBRARY
#define STANDARD_LIBRARY

#include <iostream>
#include <chrono>
#include <thread>
...
// Everything I need in the project
#endif
Run Code Online (Sandbox Code Playgroud)

并在任何地方包含这个单一的标题,我需要的东西 std

我能想到的问题是:

  1. C库函数对名称空间的污染不需要在std名称空间中.
  2. 编译时间较慢

但我没有遇到过1. sofar的重大问题.几乎所有东西都在std命名空间中.我也不完全理解为什么2.必然是一个重大问题.标准标题很少改变.另据我所知,编译器可以预编译它们.当涉及到模板时,它们只在我需要时才被实例化(编译).

还有一些好处:

  1. 减少打字
  2. 少阅读
  3. 少弄清楚我需要哪些标题以及某个函数的标题

我是一个没有大型项目经验的初学者,我真的很想弄明白,所以请怜悯我.

c++ coding-style

28
推荐指数
1
解决办法
3483
查看次数

为什么<cmath>中的某些函数不在std命名空间中?

我正在开发一个适用于多种算术类型的项目.所以我制作了一个标题,其中定义了用户定义的算术类型的最低要求:

user_defined_arithmetic.h:

typedef double ArithmeticF;   // The user chooses what type he 
                              // wants to use to represent a real number

namespace arithmetic          // and defines the functions related to that type
{

const ArithmeticF sin(const ArithmeticF& x);
const ArithmeticF cos(const ArithmeticF& x);
const ArithmeticF tan(const ArithmeticF& x);
...
}
Run Code Online (Sandbox Code Playgroud)

令我不安的是,当我使用这样的代码时:

#include "user_defined_arithmetic.h"

void some_function()
{
    using namespace arithmetic;
    ArithmeticF lala(3);
    sin(lala);
}
Run Code Online (Sandbox Code Playgroud)

我收到编译器错误:

error: call of overloaded 'sin(ArithmeticF&)' is ambiguous
candidates are:
double sin(double)
const ArithmeticF arithmetic::sin(const ArithmeticF&)
Run Code Online (Sandbox Code Playgroud)

我从来没有使用过 …

c++ namespaces cmath

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

如何在LibreOffice中运行python宏?

当我转到工具 - >宏 - >组织宏 - > Python时,我得到了这个对话框:

在此输入图像描述

这是不可能创造新的Python宏.

显然LibreOffice 没有Python编辑器,因此我必须在其他地方编写宏,然后执行它们.

但我不知道在哪里放Python脚本.

我尝试在系统范围内搜索名称中包含"HeloWorld"的文件,我没有得到任何结果.

我试图将test.py文件放入:

/home/martin/.config/libreoffice/4/user/Scripts
Run Code Online (Sandbox Code Playgroud)

并重新加载应用程序,但不显示测试宏.

我试图在工具 - >选项 - >路径中找到适当的设置,但没有"宏路径":

在此输入图像描述

如何从LibreOffice运行Python宏?

这个问题是我学习Python和学习LibreOffice宏的一部分,因此欢迎任何指向常规教程的链接.关于LibreOffice Python宏的教程似乎特别难以找到.

我使用的是LibreOffice版本:4.1.3.2

python macros ubuntu libreoffice ubuntu-13.10

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

受保护的成员在派生类中"未在此范围内声明"

#include <vector>
#include <iostream>

template <class T>
class Base
{
protected:
    std::vector<T> data_;
};

template <class T>
class Derived : public Base<T>
{
public:
    void clear()
    {
        data_.clear();
    }
};

int main(int argc, char *argv[])
{
    Derived<int> derived;
    derived.clear();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我无法编译这个程序.我明白了:

main.cpp:22: error: 'data_' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

拜托,你能解释为什么data_Derived课堂上看不到?

c++ inheritance templates

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

shuffle和random_shuffle c ++有什么区别

函数std :: shuffle已在C++ 11中引入:

template< class RandomIt, class URNG >
void shuffle( RandomIt first, RandomIt last, URNG&& g );
Run Code Online (Sandbox Code Playgroud)

它与std :: random_shuffle的重载之一具有相同的签名,这也是在C++ 11中引入的:

template< class RandomIt, class RandomFunc >
void random_shuffle( RandomIt first, RandomIt last, RandomFunc&& r );
Run Code Online (Sandbox Code Playgroud)

不同之处在于第三个参数,其中:

URNG必须满足UniformRandomNumberGenerator的要求

这都是?差异只是shuffle执行额外的编译时间检查吗?这个行为是否相同?

c++ c++-standard-library c++11

23
推荐指数
2
解决办法
6571
查看次数