是否可以声明一个可以指向任何类的成员函数的函数指针(非 C++ 11)(阅读:不是特定的类)?
例如,如果我有类 A、B 和 C。C 中声明了一个函数指针,我想在指向 B 的成员函数之一和 A 的成员函数之一之间切换该指针。C++ 允许这样做吗?
我在几个地方使用随机数,并且通常在需要时构建一个随机数生成器。目前,我使用 Marsaglia Xorshift 算法将当前系统时间作为种子。现在我对这个策略有一些疑问:如果我使用多个生成器,生成器之间的数字的独立性(随机性)取决于种子(相同的种子相同的数字)。由于我使用时间(ns)作为种子,并且由于这次改变了这个工作,但我想知道仅使用一个单一生成器是否会更好,例如使其可用作单例。这会提高随机数质量吗?
编辑:不幸的是 c++11 还不是一个选项
编辑:更具体地说:我并不是建议单例可以提高随机数质量,而是仅使用一个生成器并播种这一事实。否则,我必须确保不同生成器的种子彼此独立(随机)。极端的例子:我用完全相同的数字播种两个生成器 - >它们之间没有随机性
我正在开发一个需要与 C++03 向后兼容的遗留库,但也向前兼容以利用 C++11 功能,如移动语义和显式转换。
那么,是否可以在 C++03 中模拟显式转换?我显然知道显式 bool(或“安全”bool)习惯用法 - 但这仅用于转换为布尔类型。是否可以在 C++03 中模拟通用的显式转换运算符?
我查了一下,在一本名为“不完美的 C++:现实生活编程的实用解决方案”的书中找到了关于这个的讨论。
在这本书中,他们讨论了在 C++03(这本书是在 C++11 之前编写的)中模拟显式强制转换的一些想法。最终,他们建议创建一个explicit_cast<T>模板。但是,我不喜欢该解决方案,因为我希望用户能够简单地使用static_cast<T>,这在 C++11 中运行良好。
因此,另一种解决方案是强制编译器进行两次转换,这将禁止隐式转换。一个例子是这样的:
class int_cast
{
public:
int_cast(const int& v) : m_value(v)
{ }
operator int() const
{
return m_value;
}
private:
int m_value;
};
struct Foo
{
Foo()
{
x = 10;
}
operator int_cast() const
{
return int_cast(x);
}
int x;
};
Run Code Online (Sandbox Code Playgroud)
在这里, aFoo应该显式转换为int,但不能隐式 …
在大量使用 Haskell 和函数式语言之后,我开始学习 C++,我发现我一直在尝试解决同样的问题:
如果这是 Haskell,我可以简单地利用一切都是懒惰的事实,并在我想到的时候编写我的转换,然后它会在下游被消耗时应用。甚至有一些库可以执行这种精确的模式(导管和管道)。
假设我想获取序列1 2 3 4 5 6 ...和输出12 34 56 ...。我可以了解如何编写在流上运行并就地处理数据的临时代码。但我想知道是否存在一种抽象机制,允许我通过转换来自另一个流的数据(以任何可以想到的方式)来构建新的流。这种抽象应该允许我在处理数据时缓冲数据,而不仅仅是单个元素到新值的简单映射。
以下是限制:
如果你在想,这是作业吗?好吧,我收到了很多类作业,这些作业要求我处理数据流(这就是没有库和 C++03 限制的原因)。并不是我不知道如何使用while循环来做到这一点,而是我想知道 stl 中是否存在现有的流抽象,只是等待被发现和使用。
但如果唯一的方法是使用 C++11,那么我想知道。
我在代码库的各个位置都有以下层次结构模式:
enum DerivedType {
A, B, C };
class Base {
public:
static Base* Create(DerivedType t);
};
template <DerivedType T>
class Derived : public Base {
};
Run Code Online (Sandbox Code Playgroud)
该方法返回类、或 Create的新对象,具体取决于其参数:Derived<A>Derived<B>Derived<C>
Base* Base::Create(DerivedType t) {
switch (t) {
case A: return new Derived<A>;
case B: return new Derived<B>;
case C: return new Derived<C>;
default: return NULL;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是有很多这样的层次结构,基本上到处都有Base -> Derived相同的复制粘贴实现。Create()有没有一种优雅且易于理解的方法来避免重复?
我有一个需要打印出来的测试std::pair<std::string, std::string>,但即使我为该 googletest 声明并定义了一个运算符,它也会抱怨它找不到它。
谷歌测试手册提到了这一点
\n// It\'s important that the << operator is defined in the SAME\n// namespace that defines Bar. C++\'s look-up rules rely on that.\nRun Code Online (Sandbox Code Playgroud)\n我应该重新打开std命名空间并将其放置operator<<(std::ostream& os, const std::pair<std::string, std::string>& p)在那里吗?
代码摘录:
\n// included from somewhere else in legacy code:\n// typedef ::std::map< ::std::string, ::std::string > AttrList;\nbool UserDefinedTypeMatcher::MatchAndExplain(UserDefinedType& r, testing::MatchResultListener* listener) const {\n typedef AttrList::const_iterator AttrIt;\n std::pair<AttrIt, AttrIt> pattr = std::mismatch(r.attr.begin(), r.attr.end(), expectedUserDefinedType.attr.begin());\n if(pattr.first != r.attr.end() && …Run Code Online (Sandbox Code Playgroud) 我编写了一个模板Interval类,我想将其用作数字间隔的容器。由于我想迭代此类中实际ItervalIter包含的元素,因此我添加了一个模板友元类。
当我在 Linux 上使用 编译项目以及gcc在 MacOS 上使用 编译项目时clang,一切都编译得很好,没有警告。当我在 Windows 上使用 进行编译时msvc,出现以下错误消息:
C2373 'utils::Interval<T>::begin': redefinition. The type modifiers are different
C2447 '{': missing function header (old-style formal list?)
C2373 'utils::Interval<T>::end': redefinition. The type modifiers are different
C2447 '{': missing function header (old-style formal list?)
Run Code Online (Sandbox Code Playgroud)
我简化了该类的实现Interval,以获得以下MRE:
间隔.h
#pragma once
#ifndef INTERVAL_H_INCLUDED
#define INTERVAL_H_INCLUDED
namespace utils {
template <class T> class IntervalIter;
//-----------------------------------------------------------------------------
// Interval
//-----------------------------------------------------------------------------
template <class …Run Code Online (Sandbox Code Playgroud) Visual Studio 2019 的最新 16.6 更新删除了std::plus::result_type、std::minus::result_type和相关的 typedef。(它们在 C++17 中已弃用,并在 C++20 中删除。)代码的大大简化版本如下所示:
template <typename FF>
struct function_wrapper {
function_wrapper(FF func, const std::string& name) : func_(func), name_(name) { }
int operator()(int i1, int i2) const { return func_(i1, i2); }
// ... other stuff ...
FF func_;
std::string name_;
};
template <typename FF>
int use_function(const function_wrapper<FF>& func, const std::pair<int, int>& args) {
return func(args.first, args.second);
}
funcWrapper<boost::function<int(int, int)>> plus_func(std::plus<int>(), "plus");
std::cout << use_function(plus_func, std::make_pair<int, int>(1, 2)) << std::endl; …Run Code Online (Sandbox Code Playgroud) 我有一个向量类:
template <typename T>
class Vector2 {
public:
T x, y;
Vector2(T x, T, y) : x{x}, y{y} {}
}
Run Code Online (Sandbox Code Playgroud)
我想根据上下文获得不同的访问器:
Vector2<float, x, y> xy;
xy.x = 42;
Vector2<float, u, v> uv;
uv.u = 42;
Run Code Online (Sandbox Code Playgroud)
在 C++ 中可能有类似的事情吗?更具体地说,这在 C++03 中可能吗?
我使用 C++ 开发用于 DC/DC 电压转换器的嵌入式固件。我有不同的值,例如电压、电流...可以将一些变换从三相电流(u,v,w)应用到两相静态(a,b)和两相旋转坐标系(d,q) )。使用特定类型会更具可读性,例如:
using CurrentDQ = Vector2<float, d, q>
using CurrentAB = Vector2<float, a, b>
using Current3 = Vector<float, u, v, w>
Run Code Online (Sandbox Code Playgroud) 如何判断给定参数是否是C++ 03中的右值?我正在编写一些非常通用的代码,如果可能的话我需要参考,否则就构建一个新的对象.我可以重载以获取by-value和by-reference并让rvalue返回调用by-value函数吗?
或者我有一种非常令人作呕的感觉,这就是为什么右值引用在C++ 0x中?
编辑:
is_rvalue =!(is_reference || is_pointer)?
c++ ×10
c++03 ×10
templates ×4
boost-bind ×1
c++11 ×1
c++20 ×1
c++98 ×1
constants ×1
factory ×1
googlemock ×1
googletest ×1
lvalue ×1
random ×1
rvalue ×1
stream ×1
visual-c++ ×1