未定义类型的重载

Overload for undefined type

我正在尝试对模板函数进行一些重载,以下是示例

do_something.h

template<typename T>
void do_something(T const &input){/*....*/}

void do_something(std::string const &input);

void do_something(boost::container::string const &input);

到目前为止一切顺利,但是如果我想重载一个未定义的类型怎么办?

喜欢使用头文件中没有定义的类型some_type

void do_something(some_type const &input);

我想这样用

main.cpp

#include "do_something.h"
#include "some_type.h"

#include <boost/container/string.hpp>

int main()
{
     do_something(std::string("whatever"));
     do_something(boost::container::string("whatever"));

     //oops, some_type() never defined in the header file, this
     //function will call the template version, but this is not
     //the behavior user expected
     do_something(some_type());   
}

因为 some_type 不是 POD,也不是 std::string,boost::container::string。我想我可以设计一个特征来做一些编译时检查

template<typename T>
typename boost::enable_if<is_some_type<T>::value, T>::type
do_something(T const &input){//.....}

但是我有更好的方法吗?

我需要编译时类型检查,所以我使用 template.All 的类型调用这个函数会根据不同的类型做类似的工作,所以我更喜欢 overload.I 不需要保存状态,所以我更喜欢函数而不是 class。 希望这可以帮助您更多地了解我对 do.Thank 您

的意图

but what if I want to overload a non-defined type?

您需要提供

的声明
void do_something(some_type const &input);

在使用 some_type 类型的对象调用 do_something 之前。否则,将使用模板版本。

#include "do_something.h"
#include "some_type.h"

// This is all you need. You can implement the function here
// or any other place of your choice.
void do_something(some_type const &input);

#include <boost/container/string.hpp>

int main()
{
     do_something(std::string("whatever"));
     do_something(boost::container::string("whatever"));

     //oops, some_type() never defined in the header file, this
     //function will call the template version, but this is not
     //the behavior user expected
     do_something(some_type());   
}