函数重载错误
我不明白为什么代码会这样?
#include <iostream>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/container/vector/vector_fwd.hpp>
#include <boost/fusion/include/vector_fwd.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/sequence/io.hpp>
#include <boost/fusion/include/io.hpp>
template<typename Ar>
void func(Ar& ar, const boost::fusion::vector<>& v) {
std::cout << v << std::endl;
}
template<typename Ar, typename T0, typename T1>
void func(Ar& ar, const boost::fusion::vector<T0, T1>& v) {
std::cout << v << std::endl;
}
struct type {
template<typename T>
type& operator& (const T& v) {
func(*this, v);
return *this;
}
};
int main() {
type t;
t & boost::fusion::make_vector(33,44); // 1. <<<<<<<<<<<<<<<<<<<<<<<<
boost::fusion::vector<int, int> v(55,66); // 2.
t & v;
}
问题是,为什么在第一种情况下调用空向量的 func() ?
有关此主题的文档:
谢谢。
I can't understand why the code behaves this way?
#include <iostream>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/container/vector/vector_fwd.hpp>
#include <boost/fusion/include/vector_fwd.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/sequence/io.hpp>
#include <boost/fusion/include/io.hpp>
template<typename Ar>
void func(Ar& ar, const boost::fusion::vector<>& v) {
std::cout << v << std::endl;
}
template<typename Ar, typename T0, typename T1>
void func(Ar& ar, const boost::fusion::vector<T0, T1>& v) {
std::cout << v << std::endl;
}
struct type {
template<typename T>
type& operator& (const T& v) {
func(*this, v);
return *this;
}
};
int main() {
type t;
t & boost::fusion::make_vector(33,44); // 1. <<<<<<<<<<<<<<<<<<<<<<<<
boost::fusion::vector<int, int> v(55,66); // 2.
t & v;
}
The question is, why in the first case the func() for an empty vector is called?
Documentation on this topic:
Thank's.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这大致是我的理解...
boost::fusion::make_vector()
从您使用boost::fusion::make_vector(33,44)
返回一个boost::fusion::vector2
类型,而不是boost::fusion::vector
> (可变)类型。然而,boost::fusion::vectorN
类型可以将自身转换为boost::fusion::vector<>
(可变参数)类型。第一个函数接受 NO 类型的可变参数向量。因此不会显示任何元素。第二个版本接受声明了两种模板类型的可变参数类型,但是由于第一个版本匹配得更好(因为默认模板类型启动),所以当您使用 boost::fusion::make_vector 时,它会选择第二个版本。 。当您像第二种情况一样定义向量的类型时,它指定了强类型,因此与第二个函数匹配并显示 int 和 int 类型的两个元素。
This is roughly what I understand...
boost::fusion::make_vector()
from your usage ofboost::fusion::make_vector(33,44)
returns aboost::fusion::vector2<int, int>
type and NOT aboost::fusion::vector<int, int, T2, T3,...>
(variadic) type.boost::fusion::vectorN
types can however convert itself toboost::fusion::vector<>
(variadic) type.The first function accepts a variadic vector with NO type. Hence no elements are displayed. The second version accepts a variadic type with two template types declared, however since the first one matches better (because the default template types kick in) it gets choosen over the second one, when you use
boost::fusion::make_vector
. When you define the type of the vector as in the second case, it has strong type specified and hence matches the second function and displays two elements of type int and int.