Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将类型转换扩展到可转换类型的对/元组_C++_Type Conversion - Fatal编程技术网

C++ 将类型转换扩展到可转换类型的对/元组

C++ 将类型转换扩展到可转换类型的对/元组,c++,type-conversion,C++,Type Conversion,我必须处理一系列2D点类型:pair、pair、pair,并且我希望在存在点坐标转换时允许点之间进行隐式转换。大概是这样的: template< class From, class To > inline operator pair< To, To > ( pair< From, From > that ) { return make_pair( static_cast< To >( that.first ),

我必须处理一系列2D点类型:
pair
pair
pair
,并且我希望在存在点坐标转换时允许点之间进行隐式转换。大概是这样的:

template< class From, class To > 
inline operator pair< To, To > ( pair< From, From > that )
{
    return make_pair( static_cast< To >( that.first ), 
                    static_cast< To >( that.second ) );
}
template
内联运算符对(对that)
{
返回make_pair(static_cast(that.first),
静态_cast(that.second));
}
不幸的是,
g++
对象:

convert.cpp:5:错误:“运算符std::pair(std::pair)”必须是非静态成员函数


是否可以通过为
对定义包装类来执行上述操作?

您不能对未知类型进行隐式转换;同样,转换运算符必须是一个非静态成员函数,它仍然需要您将其包装为一个类;并从未知类型(也称为模板化构造函数)编写一个转换构造函数

您为什么不想让它成为一个免费函数:

template<typename To, typename From>
std::pair<To, To> convert(const std::pair<From, From>& p){
    return std::make_pair( static_cast< To >( p.first ), 
                static_cast< To >( p.second ) );
}
由于转换构造函数,下面的语句将全部工作

int main() {
    Point<float> mp{4.535, 395.3};
    Point<int> ip = mp;
    print(mp);
    print(ip);
    return 0;
}
intmain(){
mp点{4.53535,395.3};
点ip=mp;
印刷品(mp);
印刷(ip);
返回0;
}

请参见

否。您可以声明自己的对:双点2D、浮点2D、整数点2D,并提供适当的转换。(你可以考虑一个模板,这里)一个转换函数不带参数。您可以将其设置为常规(可能是静态)成员函数。包装器的另一种替代方法是从
pair
派生,但这样做也有很多问题。我认为您的操作符即使作为成员函数也不会隐式工作,因为返回类型中的模板参数不能从函数参数中推断出来(或者隐式是指一些盲目的
static\u cast
where
t=pair
?)。最后,我的观点是:隐式转换通常会降低可读性,因此最好将其作为一个函数保留(同样,除非您指的是
static\u cast
),因为我不想用
convert
语句污染代码的其余部分。如果函数
foo
接受参数
pair
,变量
bar
被定义为
pair
,我希望调用看起来像
foo(bar)
,而不是
foo(convert(bar))
@Michael,唯一的解决方法是将它包装在一个类中。请参阅我的最新答案。怎么样
template<typename T>
class Point{
public:
    T x = T{};
    T y = T{};

    template<typename Y>
    Point(Point<Y> p) : 
        x(static_cast<T>(p.x)),
        y(static_cast<T>(p.y))
    { }

    Point(T x_val, T y_val) : x(x_val), y(y_val)
    { }

    Point(Point&&) = default;
    Point(const Point&) = default;
    Point& operator = (Point&&) = default;
    Point& operator = (const Point&) = default;
};
void print(Point<double> p){
    std::cout << "(" << p.x << ", " << p.y << ")\n";
}
int main() {
    Point<float> mp{4.535, 395.3};
    Point<int> ip = mp;
    print(mp);
    print(ip);
    return 0;
}