听起来你想重载一个函数:
void foo(int i)
{
// stuff
}
void foo(float f)
{
// stuff
}
int main(void)
{
int i = 10;
float f = 1.0f;
foo(i); // calls foo(int)
foo(f); // calls foo(float)
}
如果您想要int-特殊行为,然后在所有其他情况下需要其他行为,您可以使用模板:
template <typename T>
void foo(T t)
{
// T is something
}
template <>
void foo(int i)
{
// for int's only
}
int main(void)
{
int i = 10;
float f = 1.0f;
double d = 2.0;
foo(i); // calls specialized foo
foo(f); // calls generic foo
foo(d); // calls generic foo
}
根据您的评论(“手头的任务是一个简单的程序:获取两个用户输入的整数并将它们相加。将输入限制为仅整数。我可以在 Python 中做到这一点,我也在沿着这些思路思考。 if num1 != type(int): print "You didn't enter an integer, please enter a integer." else: continue"),你想要这样的:
#include <iostream>
int main(void)
{
int i;
std::cin >> i;
if (std::cin.fail())
{
std::cout << "Not valid!" << std::endl;
}
else
{
// ...
}
}
这将通知诸如“@#$”、“r13”之类的无效输入,但不会捕获诸如“34fg”、“12$#%”之类的情况,因为它会读取int,分别停在“fg”和“$#%”。
要检查这一点,您必须读入一行输入,然后尝试将该行转换为您想要的类型。 (谢谢,litb)。这意味着您的问题更多like this question:
#include <iostream>
#include <sstream>
#include <string>
int main(void)
{
std::string input;
std::getline(std::cin, input);
std::stringstream ss(input);
int i;
ss >> i;
if (ss.fail() || !(ss >> std::ws).eof())
{
std::cout << "Not valid!" << std::endl;
}
else
{
// ...
}
}
它执行以下操作:获取输入,并将其放入stringstream。然后在解析int 之后,流出任何剩余的空白。之后,如果eof 为假,则表示有剩余字符;输入无效。
这更容易使用包装在一个函数中。在另一个问题中,演员阵容被重新考虑了。在这个问题中,我们使用演员表,但将输入与它一起包装。
#include <iostream>
#include <sstream>
#include <string>
bool parse_int(int& i)
{
std::string input;
std::getline(std::cin, input);
std::stringstream ss(input);
ss >> i;
return !(ss.fail() || !(ss >> std::ws).eof());
}
int main(void)
{
int i;
if (!parse_int(i))
{
std::cout << "Not valid!" << std::endl;
}
else
{
// ...
}
}
或更笼统地说:
#include <iostream>
#include <sstream>
#include <string>
template <typename T>
bool parse_type(T& t)
{
std::string input;
std::getline(std::cin, input);
std::stringstream ss(input);
ss >> t;
return !(ss.fail() || !(ss >> std::ws).eof());
}
int main(void)
{
int i;
if (!parse_type(i))
{
std::cout << "Not valid!" << std::endl;
}
else
{
// ...
}
}
这让您可以通过错误检查解析其他类型。
如果您可以接受异常,使用 lexical_cast(来自 boost 或“伪造”,请参阅代码中链接的另一个问题 [与上述链接相同]),您的代码将如下所示:
#include <iostream>
#include <sstream>
#include <string>
/* Faked lexical-cast from question:
https://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c/
*/
template <typename T>
T lexical_cast(const std::string& s)
{
std::stringstream ss(s);
T result;
if ((ss >> result).fail() || !(ss >> std::ws).eof())
{
throw std::bad_cast("Bad cast.");
}
return result;
}
template <typename T>
T parse_type(void)
{
std::string input;
std::getline(std::cin, input);
return lexical_cast<T>(input);
}
int main(void)
{
try
{
int i = parse_type<int>();
float f = parse_type<float>();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
我认为 boost 没有词法转换的无抛出版本,因此我们可以通过捕获 bad_cast 来制作此代码的真/假而不是异常版本,如下所示。再一次,这适用于 boost 或自定义词法转换。 (任何进行词法转换并抛出 bad_cast 的东西):
#include <iostream>
#include <sstream>
#include <string>
/* Faked lexical-cast from question:
https://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c/
*/
template <typename T>
T lexical_cast(const std::string& s)
{
std::stringstream ss(s);
T result;
if ((ss >> result).fail() || !(ss >> std::ws).eof())
{
throw std::bad_cast("Bad cast.");
}
return result;
}
template <typename T>
bool parse_type(T& t)
{
std::string input;
std::getline(std::cin, input);
try
{
t = lexical_cast<T>(input);
return true;
}
catch (const std::bad_cast& e)
{
return false;
}
}
int main(void)
{
int i;
if (!parse_type(i))
{
std::cout << "Bad cast." << std::endl;
}
}
现在返回到 bool 结果,除了我们通过使用现有的 lexical_cast 函数来避免代码重复。
您当然可以选择您想使用的方法。