我有一组名为的可交换二进制函数的重载overlap,它接受两种不同的类型:
class A a; class B b;
bool overlap(A, B);
bool overlap(B, A);
Run Code Online (Sandbox Code Playgroud)
overlap当且仅当一个形状与另一个形状重叠时,我的函数返回true - 这是讨论multimethods时使用的一个常见示例.
因为overlap(a, b)相当于overlap(b, a),我只需要实现关系的一个"侧".一个重复的解决方案是写这样的东西:
bool overlap(A a, B b) { /* check for overlap */ }
bool overlap(B b, A a) { return overlap(a, b); }
Run Code Online (Sandbox Code Playgroud)
但我宁愿不N! / 2使用模板生成相同功能的额外简单版本.
template <typename T, typename U>
bool overlap(T&& t, U&& u)
{ return overlap(std::forward<U>(u), std::forward<T>(t)); }
Run Code Online (Sandbox Code Playgroud)
不幸的是,这很容易无限递归,这是不可接受的:见 http://coliru.stacked-crooked.com/a/20851835593bd557
如何防止这种无限递归?我正确地解决了这个问题吗?
看看下面的伪 C++ 代码:
typedef *** SomeType1;
typedef *** SomeType2;
typedef *** SomeType3;
void BFunc(SomeType1& st1, SomeType2& st2, SomeType3& st3)
{
/*some work*/;
}
template <typename T1, typename T2, typename T3>
void AFunc(T1& p1, T2& p2, T3& p3)
{
BFunc(???);
}
Run Code Online (Sandbox Code Playgroud)
有两个带参数的函数。参数计数大于三个,但为了简单起见,例如让它为三个。
的Afunc-它是模板函数具有相同的参数当作BFunc加上参数具有相同类型的BFunc参数。但是(!)参数的顺序BFunc可以(或不能)不同。例如:
BFunc(int, double, char)
AFunc<double, int, char>
AFunc<int, double, char>
AFunc<char, double, int>
AFunc<char, int, double>
...
Run Code Online (Sandbox Code Playgroud)
那么如何重新排序内部参数AFunc以BFunc使用正确的参数序列进行调用?
我有一组类A,B,C,我希望按类型fe的通用代码访问它们的实例
template<typename T>
newObject()
{
return m_storage->getNew();
}
Run Code Online (Sandbox Code Playgroud)
其中m_storage是A或B或C的实例,取决于T.
所以我提出了std :: tuple,但是有问题因为我无法按类型从元组中获取元素.
std::tuple<A,B,C> m_tpl;
template<typename T>
newObject()
{
return m_tpl.get<T>().getNew();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?这可能吗?
谢谢.
PS:我不想为每种类型编写newObject的特化.:-)
考虑以下代码:
#include <iostream>
struct A {
A(int n, char c, bool b)
/* : some complex initialization list that you don't want to repeat. */
{initialize();}
A(int n) : A(n, default_char, default_bool) {}
A(char c) : A(default_int, c, default_bool) {} // Do not want initialize() called!
A(bool b) : A(default_int, default_char, b) {}
A(int n, char c) : A(n, c, default_bool) {}
A(int n, bool b) : A(n, default_char, b) {} // Do not want initialize() called!
A(char c, bool …Run Code Online (Sandbox Code Playgroud) 我有一个结构Dimensions,其中包含宽度和高度的模板参数:
template<unsigned W, unsigned H>
struct Dimensions
{
static constexpr unsigned width = W;
static constexpr unsigned height = H;
};
Run Code Online (Sandbox Code Playgroud)
我有Width和Height类有各自值的模板参数:
template<unsigned N>
struct Width
{
static constexpr unsigned value = N;
};
template<unsigned N>
struct Height
{
static constexpr unsigned value = N;
};
Run Code Online (Sandbox Code Playgroud)
我已经为宽度和高度创建了用户定义的文字
template<char... cs>
constexpr auto operator""_w() -> Width<to_unsigned(0, parse(cs)...)>
{
return {};
}
template<char... cs>
constexpr auto operator""_h() -> Height<to_unsigned(0, parse(cs)...)>
{
return {};
}
Run Code Online (Sandbox Code Playgroud)
在哪里to_unsigned并将 …
我想知道是否有一种方法可以减少下面代码中重载函数(函数编辑)的数量。
class foo
{
public:
foo(int _a, char _b, float _c) : a(_a), b(_b), c(_c){};
void edit(int new_a);
void edit(char new_b);
void edit(float new_c);
void edit(int new_a, char new_b);
void edit(int new_a, float new_c);
void edit(char new_b, float new_c);
void edit(int new_a, char new_b, float new_c);
void info();
private:
int a;
char b;
float c;
};
Run Code Online (Sandbox Code Playgroud)
这是编辑功能的实现:
void foo::edit(int new_a)
{
a = new_a;
}
void foo::edit(char new_b)
{
b = new_b;
}
void foo::edit(float new_c)
{
c = new_c; …Run Code Online (Sandbox Code Playgroud)