我正在编写一个函数 inListi() ,它至少接受一个参数并将第一个参数与所有后续参数的列表进行比较。如果第一个参数 == 列表中的一个元素,则返回 true,否则返回 false。所以:
if( inListi( 1.2, 2.3, 4.5, 1.2))
std::cout << "Returns true because last argument equals the first argument." << endl;
if( inListi( "hello", "world", "HEllo"))
std::cout << "This should print out because of the last argument." << endl;
问题是,它不起作用。我有下面的代码。对于 char[N],我将数组的 N 部分复制到一个字符串中,然后再继续。我想这样做,因为我可能会收到一个非空终止的 char[N]。
无论如何,代码如下。大多数代码是多余的,并且处理 const 和一个参数为 const[N] 而另一个不是该类型的组合。(顺便问一下,有没有办法减少这种重复?)
#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <typeinfo>
#include <type_traits>
#include <boost/algorithm/string.hpp>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
// inListi
////////////////////////////////////////////////////////////////////////////////
template<typename T>
bool inListi(T&& value)
{
return false;
}
template<typename FirstType, typename SecondType, typename ... Rest>
bool inListi(FirstType&& first, SecondType&& second, Rest ... rest)
{
cout << "GENERIC inListi" << endl;
cout << "first is " << typeid(first).name() << endl;
cout << "second is " << typeid(second).name() << endl;
if( first == second)
return true;
else return inListi( first, rest...);
}
// We specialize the inListi for strings. We lower the case.
// but what if char[n] is passed? We have specializations that
// convert that to strings.
template<typename ... Rest>
bool inListi( string &&first, string &&second, Rest ... rest) {
string lFirst = first;
string lSecond = second;
cout << "LOWERED" << endl;
boost::algorithm::to_lower( lFirst);
boost::algorithm::to_lower( lSecond);
if( lFirst == lSecond)
return true;
else return inListi( first, rest...);
}
// Specializations for when we are given char-arrays. We copy the
// the arrays into a string upto the size of the array. This is done
// to take care of the case of when the char-array is not nul-terminated.
// The amount repetition is to permutate over which argument is a char-array
// and also for const-ness.
template<int F, typename SecondType, typename ... Rest>
bool inListi( char (&&first)[F], SecondType &&second, Rest ... rest) {
string strFirst = string( first, F);
cout << "arr, type, rest" << endl;
return inListi( strFirst, second, rest...);
}
template<int F, typename SecondType, typename ... Rest>
bool inListi( const char (&&first)[F], SecondType &&second, Rest ... rest) {
string strFirst = string( first, F);
cout << "const arr, type, rest" << endl;
return inListi( strFirst, second, rest...);
}
template<typename FirstType, int S, typename ... Rest>
bool inListi( FirstType &&first, char (&&second)[S], Rest ... rest) {
string strSecond = string( second, S);
cout << "type, arr, rest" << endl;
return inListi( first, strSecond, rest...);
}
template<typename FirstType, int S, typename ... Rest>
bool inListi( FirstType &&first, const char (&&second)[S], Rest ... rest) {
string strSecond = string( second, S);
cout << "type, const arr, rest" << endl;
return inListi( first, strSecond, rest...);
}
template<int F, int S, typename ... Rest>
bool inListi( char (&&first)[F], char (&&second)[S], Rest ... rest) {
string strFirst = string( first, F);
string strSecond = string( second, S);
cout << "arr, arr, rest" << endl;
return inListi( strFirst, strSecond, rest...);
}
template<int F, int S, typename ... Rest>
bool inListi( const char (&&first)[F], char (&&second)[S], Rest ... rest) {
string strFirst = string( first, F);
string strSecond = string( second, S);
cout << "const arr, arr, rest" << endl;
return inListi( strFirst, strSecond, rest...);
}
template<int F, int S, typename ... Rest>
bool inListi( char (&&first)[F], const char (&&second)[S], Rest ... rest) {
string strFirst = string( first, F);
string strSecond = string( second, S);
cout << "arr, const arr, rest" << endl;
return inListi( strFirst, strSecond, rest...);
}
template<int F, int S, typename ... Rest>
bool inListi( const char (&&first)[F], const char (&&second)[S], Rest ... rest) {
string strFirst = string( first, F);
string strSecond = string( second, S);
cout << "const arr, const arr, rest" << endl;
return inListi( strFirst, strSecond, rest...);
}
int main() {
if( inListi( "Hello", "World", "HEllo"))
cout << "Hello is in the listi." << endl;
else
cout << "Hello is not in the listi." << endl;
return 0;
}
程序的输出如下:
[bitdiot foo]$ g++ forStackOverflow.cpp -std=gnu++0x
[bitdiot foo]$ ./a.out
GENERIC inListi
first is A6_c
second is A6_c
GENERIC inListi
first is A6_c
second is PKc
Hello is not in the listi.
请注意,没有调用任何中间代码,它直接使用通用版本。此外,另一个看起来很奇怪的是“PKc”。我假设是 char* 类型。现在,为什么它会有不同的类型?
总之,谢谢!!