Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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++_Templates - Fatal编程技术网

C++ 如何为模板化类型提供模板函数的专门化?

C++ 如何为模板化类型提供模板函数的专门化?,c++,templates,C++,Templates,为了处理boost::optional,我想提供一个专门化的模板函数。但是,如果我想让我的专业化处理所有类型的boost::optional,而不是像boost::optional这样的特定类型,我似乎无法为这个场景找到合适的语法 下面是一个可编译的示例: #include <boost/optional.hpp> template <typename T> void foo(const T& t) {} // This works. template<

为了处理
boost::optional
,我想提供一个专门化的模板函数。但是,如果我想让我的专业化处理所有类型的
boost::optional
,而不是像
boost::optional
这样的特定类型,我似乎无法为这个场景找到合适的语法

下面是一个可编译的示例:

#include <boost/optional.hpp>

template <typename T>
void foo(const T& t)
{}

// This works.
template<>
void foo<int>(const int& t)
{}

// This works.
template<>
void foo<boost::optional<int>>(const boost::optional<int>& t)
{}

// What to do in this case??
// template <typename U>
// void foo<boost::optional<U>>(const boost::optional<U>& t)
// {}

int main() {}
#包括
样板
void foo(施工测试与测试)
{}
//这很有效。
样板
void foo(const int&t)
{}
//这很有效。
样板
void foo(const boost::可选&t)
{}
//在这种情况下该怎么办??
//模板
//void foo(const boost::可选&t)
// {}
int main(){}

不要专门化。而是提供一个重载

template <typename U>
void foo(const boost::optional<U>& t)
{
}
模板
void foo(const boost::可选&t)
{
}

不要专门化。而是提供一个重载

template <typename U>
void foo(const boost::optional<U>& t)
{
}
模板
void foo(const boost::可选&t)
{
}

不能部分专门化模板函数。但是你可以为

namespace detail
{
    template <typename T>
    struct foo_impl
    {
        void operator () (const T&) const {};
    }

    template <>
    struct foo_impl<int> // Full specialization for int
    {
        void operator () (int) const {};
    }

    template <>
    struct foo_impl<boost::optional<int>> // Full specialization for boost::optional<int>
    {
        void operator () (const boost::optional<int>&) const {};
    }

    template <typename T>
    struct foo_impl<boost::optional<T>> // Partial specialization for boost::optional<T>
    {
        void operator () (const boost::optional<T>&) const {};
    }

}

template <typename T>
void foo(const T& t)
{
    detail::foo_impl<T>{}(t); // Forward to correct struct
}
名称空间详细信息
{
样板
结构foo_impl
{
void操作符()(const T&)const{};
}
样板
struct foo_impl//int的完全专门化
{
void运算符()(int)const{};
}
样板
struct foo_impl//boost的完全专门化::可选
{
void操作符()(const boost::optional&)const{};
}
样板
struct foo_impl//boost的部分专门化::可选
{
void操作符()(const boost::optional&)const{};
}
}
样板
void foo(施工测试与测试)
{
detail::foo_impl{}(t);//转发到正确的结构
}
否则可以提供重载(可能更简单)

模板
void foo(const T&){}
void foo(int){}
void foo(const boost::可选&){}
样板
void foo(const boost::可选&){}
关于重载方法的注意事项:

- `foo(42)` and `foo<int>(42)` won't call the same function
- and similarly, with `boost::optional<int> opt_i;`,  
    `foo(opt_i)`, `foo<int>(opt_i)` and `foo<boost::optional<int>>(opt_i)`
     will call 3 different functions.
-`foo(42)`和`foo(42)`不会调用同一个函数
-类似地,对于boost::optional opt_i;`,
`foo(opt_i)`、`foo(opt_i)`和`foo(opt_i)``
将调用3个不同的函数。

不能部分专门化模板函数。但是你可以为

namespace detail
{
    template <typename T>
    struct foo_impl
    {
        void operator () (const T&) const {};
    }

    template <>
    struct foo_impl<int> // Full specialization for int
    {
        void operator () (int) const {};
    }

    template <>
    struct foo_impl<boost::optional<int>> // Full specialization for boost::optional<int>
    {
        void operator () (const boost::optional<int>&) const {};
    }

    template <typename T>
    struct foo_impl<boost::optional<T>> // Partial specialization for boost::optional<T>
    {
        void operator () (const boost::optional<T>&) const {};
    }

}

template <typename T>
void foo(const T& t)
{
    detail::foo_impl<T>{}(t); // Forward to correct struct
}
名称空间详细信息
{
样板
结构foo_impl
{
void操作符()(const T&)const{};
}
样板
struct foo_impl//int的完全专门化
{
void运算符()(int)const{};
}
样板
struct foo_impl//boost的完全专门化::可选
{
void操作符()(const boost::optional&)const{};
}
样板
struct foo_impl//boost的部分专门化::可选
{
void操作符()(const boost::optional&)const{};
}
}
样板
void foo(施工测试与测试)
{
detail::foo_impl{}(t);//转发到正确的结构
}
否则可以提供重载(可能更简单)

模板
void foo(const T&){}
void foo(int){}
void foo(const boost::可选&){}
样板
void foo(const boost::可选&){}
关于重载方法的注意事项:

- `foo(42)` and `foo<int>(42)` won't call the same function
- and similarly, with `boost::optional<int> opt_i;`,  
    `foo(opt_i)`, `foo<int>(opt_i)` and `foo<boost::optional<int>>(opt_i)`
     will call 3 different functions.
-`foo(42)`和`foo(42)`不会调用同一个函数
-类似地,对于boost::optional opt_i;`,
`foo(opt_i)`、`foo(opt_i)`和`foo(opt_i)``
将调用3个不同的函数。

我正要问一个后续问题,当函数的返回类型包含模板参数,并且函数没有参数列表时,我如何才能做到这一点。因为返回类型不参与重载解析,所以我不能在这个场景中应用其他答案。但是,看起来这可能适用于第二个问题…我正要问一个后续问题,当函数的返回类型包含模板参数,并且函数没有参数列表时,如何执行此操作。因为返回类型不参与重载解析,所以我不能在这个场景中应用其他答案。然而,看起来这可能对第二个问题有效。。。