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

C++ 未定义类型的重载

C++ 未定义类型的重载,c++,boost,C++,Boost,我正在尝试对模板函数进行重载,下面是一些示例 做点什么 template<typename T> void do_something(T const &input){/*....*/} void do_something(std::string const &input); void do_something(boost::container::string const &input); 我想这样使用它 main.cpp #include "do_som

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

做点什么

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

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

void do_something(boost::container::string 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());   
}
#包括“do_something.h”
#包括“some_type.h”
#包括
int main()
{
做某事(std::string(“随便”);
做点什么(boost::container::string(“whatever”);
//哎呀,有些_type()从未在头文件中定义过,这是
//函数将调用模板版本,但这不是
//用户期望的行为
做点什么(某种类型());
}
因为某些类型不是POD,不是std::string,boost::container::string。我想我可以设计一个traits来进行编译时检查

template<typename T>
typename boost::enable_if<is_some_type<T>::value, T>::type
do_something(T const &input){//.....}
模板
typename boost::enable_if::type
做某事(T常量和输入){/..}
但是我有更好的方法吗

我需要编译时类型检查,所以我使用模板。所有调用此函数的类型都会根据不同的类型执行类似的任务,所以我更喜欢重载。我不需要保存状态,所以我更喜欢函数而不是类。 希望这能帮助你更多地了解我的意图。谢谢

但是如果我想重载一个未定义的类型呢

您需要提供以下声明:

void do_something(some_type const &input);
在调用
之前,使用类型为
some\u类型的对象执行一些操作。否则,将使用模板版本

#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());   
}
#包括“do_something.h”
#包括“some_type.h”
//这就是你所需要的。您可以在这里实现该功能
//或者你选择的任何其他地方。
无效做某事(某些类型的常量和输入);
#包括
int main()
{
做某事(std::string(“随便”);
做点什么(boost::container::string(“whatever”);
//哎呀,有些_type()从未在头文件中定义过,这是
//函数将调用模板版本,但这不是
//用户期望的行为
做点什么(某种类型());
}
但是如果我想重载一个未定义的类型呢

您需要提供以下声明:

void do_something(some_type const &input);
在调用
之前,使用类型为
some\u类型的对象执行一些操作。否则,将使用模板版本

#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());   
}
#包括“do_something.h”
#包括“some_type.h”
//这就是你所需要的。您可以在这里实现该功能
//或者你选择的任何其他地方。
无效做某事(某些类型的常量和输入);
#包括
int main()
{
做某事(std::string(“随便”);
做点什么(boost::container::string(“whatever”);
//哎呀,有些_type()从未在头文件中定义过,这是
//函数将调用模板版本,但这不是
//用户期望的行为
做点什么(某种类型());
}
但是如果我想重载一个未定义的类型呢

您需要提供以下声明:

void do_something(some_type const &input);
在调用
之前,使用类型为
some\u类型的对象执行一些操作。否则,将使用模板版本

#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());   
}
#包括“do_something.h”
#包括“some_type.h”
//这就是你所需要的。您可以在这里实现该功能
//或者你选择的任何其他地方。
无效做某事(某些类型的常量和输入);
#包括
int main()
{
做某事(std::string(“随便”);
做点什么(boost::container::string(“whatever”);
//哎呀,有些_type()从未在头文件中定义过,这是
//函数将调用模板版本,但这不是
//用户期望的行为
做点什么(某种类型());
}
但是如果我想重载一个未定义的类型呢

您需要提供以下声明:

void do_something(some_type const &input);
在调用
之前,使用类型为
some\u类型的对象执行一些操作。否则,将使用模板版本

#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());   
}
#包括“do_something.h”
#包括“some_type.h”
//这就是你所需要的。您可以在这里实现该功能
//或者你选择的任何其他地方。
无效做某事(某些类型的常量和输入);
#包括
int main()
{
做某事(std::string(“随便”);
做点什么(boost::container::string(“whatever”);
//哎呀,有些_type()从未在头文件中定义过,这是
//函数将调用模板版本,但这不是
//用户期望的行为
做点什么(某种类型());
}

我不确定,你能在模板中使用静态断言吗?我不确定,你能在模板中使用静态断言吗?我不确定,你能在模板中使用静态断言吗?我不确定,你能在模板中使用静态断言吗?