我想做这样的事情:
#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?
#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) 我使用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()
示例中的函数可以假装是虚拟的,因为它是在这里执行类的自定义.因此它应该是私人的.
第二个例子很糟糕,因为:
我们不应该干涉公共接口来执行自定义. …
我想了解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) 由于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 通过引用返回吗?
我正在阅读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. sofar的重大问题.几乎所有东西都在std命名空间中.我也不完全理解为什么2.必然是一个重大问题.标准标题很少改变.另据我所知,编译器可以预编译它们.当涉及到模板时,它们只在我需要时才被实例化(编译).
还有一些好处:
我是一个没有大型项目经验的初学者,我真的很想弄明白,所以请怜悯我.
我正在开发一个适用于多种算术类型的项目.所以我制作了一个标题,其中定义了用户定义的算术类型的最低要求:
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)
我从来没有使用过 …
当我转到工具 - >宏 - >组织宏 - > 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
#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
课堂上看不到?
函数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++ ×9
c++11 ×4
coding-style ×2
cmath ×1
inheritance ×1
lambda ×1
libreoffice ×1
macros ×1
namespaces ×1
oop ×1
python ×1
stl ×1
templates ×1
ubuntu ×1
ubuntu-13.10 ×1