我想知道为什么隐式类型转换不适用于类模板上的外部运算符重载.这是工作的非模板化版本:
class foo
{
public:
foo() = default;
foo(int that)
{}
foo& operator +=(foo rhs)
{
return *this;
}
};
foo operator +(foo lhs, foo rhs)
{
lhs += rhs;
return lhs;
}
Run Code Online (Sandbox Code Playgroud)
正如所料,以下行正确编译:
foo f, g;
f = f + g; // OK
f += 5; // OK
f = f + 5; // OK
f = 5 + f; // OK
Run Code Online (Sandbox Code Playgroud)
另一方面,当类foo声明为这样的简单模板时:
template< typename T >
class foo
{
public:
foo() = default;
foo(int that)
{} …Run Code Online (Sandbox Code Playgroud) 我目前正在为元组编写算术运算符重载.运算符迭代元组以对其每个元素执行操作.这是operator + =的定义:
template< typename... Ts, std::size_t I = 0 >
inline typename std::enable_if< I == sizeof... (Ts), std::tuple< Ts... >& >::type operator +=(std::tuple< Ts... >& lhs, const std::tuple< Ts... >& rhs)
{
return lhs;
}
template< typename... Ts, std::size_t I = 0 >
inline typename std::enable_if< I != sizeof... (Ts), std::tuple< Ts... >& >::type operator +=(std::tuple< Ts... >& lhs, const std::tuple< Ts... >& rhs)
{
std::get< I >(lhs) += std::get< I >(rhs);
return operator +=< Ts..., I + 1 …Run Code Online (Sandbox Code Playgroud) 我目前正在编写一个通用的矢量模板类(几何实体,而不是容器),带有以下签名......
template< typename T, unsigned N >
class vector
{...}
Run Code Online (Sandbox Code Playgroud)
...其中T是算术类型,N是维度.我想将交叉乘积定义为运算符的重载^(位于类定义内)并仅在N == 3时启用它.我现在拥有的是:
typename boost::lazy_enable_if_c< (N == 3), vector >::type
inline operator ^(const vector &rhs) const
{
vector ret;
ret(0) = val_[1] * rhs(2) - val_[2] * rhs(1);
ret(1) = val_[2] * rhs(0) - val_[0] * rhs(2);
ret(2) = val_[0] * rhs(1) - val_[1] * rhs(0);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,使用N!= 3实例化此模板,即使未引用operator ^,也会产生以下错误:
error: no type named ‘type’ in ‘struct boost::lazy_enable_if_c < false, flare::math::vector < flare::math::fixed < short int, 8u >, …Run Code Online (Sandbox Code Playgroud) 我最近一直在玩Rvalue参考,我遇到了一个奇怪的问题.让我们定义一个名为Foo的简单类,其中包含vector< int >:
class Foo
{
public:
Foo(std::vector< int >&& v)
: v_(v)
{}
private:
std::vector< int > v_;
};
Run Code Online (Sandbox Code Playgroud)
Foo可以通过传递一个vector< int >临时构造一个实例,如下所示:
std::vector< int > temp;
Foo(std::move(temp));
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试单步执行此代码时,我注意到内部的向量Foo是使用copy-constructor而不是move-constructor构造的.但是,如果我这样指定构造函数:
Foo(std::vector< int >&& v)
: v_(std::move(v))
{}
Run Code Online (Sandbox Code Playgroud)
然后,v_适当地调用该成员的move-constructor .为什么会这样?为什么std::move(v)初始化列表中需要冗余?为什么编译器无法推断出调用向量移动构造函数的意图,因为相应的Foo构造函数参数被指定为Rvalue引用?
顺便说一下,我正在使用GCC 4.6和-std = c ++ 0x选项.
谢谢你的帮助.PMJ
我已经为通用信号/插槽系统编写了以下实现:
template< typename... Args >
class Signal : NonCopyable
{
public:
typedef std::function< void (Args...) > Delegate;
void connect(const Delegate& delegate);
void operator ()(Args&&... args) const;
private:
std::list< Delegate > _delegates;
};
template< typename... Args >
void Signal< Args... >::connect(const Delegate& delegate)
{
_delegates.push_front(delegate);
}
template< typename... Args >
void Signal< Args... >::operator ()(Args&&... args) const
{
for (const Delegate& delegate : _delegates)
delegate(std::forward< Args >(args)...);
}
Run Code Online (Sandbox Code Playgroud)
之后,我使用以下简单案例测试了我的课程:
Signal< int > signal;
// Case 1
signal(0);
//Case 2
int i(0); …Run Code Online (Sandbox Code Playgroud) 我刚开始学习 C++17 折叠表达式。我知道可以将折叠表达式应用于元组,如下例所示(灵感来自对这个问题的答复):
#include <iostream>
#include <tuple>
int main() {
std::tuple in{1, 2, 3, 4};
std::cout << "in = ";
std::apply([](auto&&... x) { ((std::cout << x << ' '), ...); }, in);
std::cout << std::endl;
std::multiplies<int> op;
auto out = std::apply([&](auto&& ...x) { return std::tuple{op(x, 3)...}; }, in);
std::cout << "out = ";
std::apply([](auto&&... x) { ((std::cout << x << ' '), ...); }, out);
std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
in = 1 2 3 4
out = …Run Code Online (Sandbox Code Playgroud) 我试图使用std::unique_ptr以将整数句柄存储到一些不透明的对象.为此,我定义了一个自定义删除器类型,它typedef int pointer的目的是覆盖原始指针类型int而不是int*.此过程在本网站的最后一节中进行了描述:http://asawicki.info/news_1494_unique_ptr_in_visual_c_2010.html
这是一些示例代码,以更好地说明我正在尝试做的事情:
#include <memory>
#include <iostream>
static void close(int p)
{
std::cout << p << " has been deleted!" << std::endl;
}
struct handle_deleter
{
typedef int pointer;
void operator()(pointer p) { close(p); }
};
typedef std::unique_ptr< int, handle_deleter> unique_handle;
int main(int argc, char *argv[])
{
unique_handle handle(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我使用GCC 4.7.2编译此代码时,我收到以下错误:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/memory:86:0,
from unique_ptr_test.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/unique_ptr.h: In instantiation of ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with …Run Code Online (Sandbox Code Playgroud)