问题描述
想象一下,我想使一个类似模板容器的元组作为api接口的一部分。我想将其限制为允许的类型列表以及此模板容器的实例。现在我有这样的东西:
#include <string>
#include <tuple>
#include <utility>
#include <type_traits>
template<typename T>
constexpr const bool bIsAllowedType =
std::is_same<T,bool>::value ||
std::is_same<T,void*>::value ||
std::is_same<T,double>::value ||
std::is_same<T,int64_t>::value ||
std::is_same<T,std::string>::value;
template<typename T,typename... Args>
constexpr const bool bIsAllowedArgList =
bIsAllowedType<std::remove_reference_t<T>> &&
bIsAllowedArgList<Args...>;
template<typename T>
constexpr const bool bIsAllowedArgList<T> =
bIsAllowedType<std::remove_reference_t<T>>;
template<typename... Args>
concept CAllowedArgList =
bIsAllowedArgList<Args...>;
template<CAllowedArgList... Args>
class Container
{
private:
using TupleT = std::tuple<std::decay_t<Args>...>;
TupleT data;
public:
Container() = default;
Container(Args&&... args) : data(std::forward<Args>(args)...) {}
};
int main()
{
auto test_cont = Container(1LL,2.0,std::string("three")); // Ok
auto err_cont = Container(std::wstring(L"wide string")); // Not ok,type's not allowed
return 0;
}
那么,如何使它也接受自身的实例?像这样:
int main()
{
auto test_cont = Container(1LL,Container(2.0,std::string("three")));
return 0;
}
我希望它尽可能简单,所以请限制使用第三方库(例如Boost)
解决方法
您可以通过使用部分模板专门化来进行检查。
template <typename T>
constexpr const bool bIsContainerType = false;
template <typename... Types>
constexpr const bool bIsContainerType<Container<Types...>> = true;
然后我们将其添加到bIsAllowedType
中的支票清单中
template<typename T>
constexpr const bool bIsAllowedType =
std::is_same<T,bool>::value ||
std::is_same<T,void*>::value ||
std::is_same<T,double>::value ||
std::is_same<T,int64_t>::value ||
std::is_same<T,std::string>::value ||
bIsContainerType<T>;
由于bIsAllowedType
现在需要了解Container
,这需要了解我们的概念,因此我将bIsAllowedType
更改为一个结构,以便我们可以对其进行声明。
#include <string>
#include <tuple>
#include <utility>
#include <type_traits>
template<typename T>
struct bIsAllowedType;
template<typename... T>
constexpr const bool bIsAllowedArgList =
(bIsAllowedType<std::remove_reference_t<T>>::value && ...);
template<typename... Args>
concept CAllowedArgList =
bIsAllowedArgList<Args...>;
template<CAllowedArgList... Args>
class Container
{
private:
using TupleT = std::tuple<std::decay_t<Args>...>;
TupleT data;
public:
Container() = default;
Container(Args&&... args) : data(std::forward<Args>(args)...) {}
};
template <typename T>
constexpr const bool bIsContainerType = false;
template <typename... Types>
constexpr const bool bIsContainerType<Container<Types...>> = true;
template<typename T>
struct bIsAllowedType {
static constexpr bool value =
std::is_same<T,bool>::value ||
std::is_same<T,void*>::value ||
std::is_same<T,double>::value ||
std::is_same<T,int64_t>::value ||
std::is_same<T,std::string>::value ||
bIsContainerType<T>;
};
int main()
{
auto test_cont = Container(static_cast<int64_t>(1),2.0,std::string("three")); // Ok
//auto err_cont = Container(std::wstring(L"wide string")); // Not ok,type's not allowed
auto test_cont2 = Container(static_cast<int64_t>(1),Container(2.0,std::string("three")));
return 0;
}
实时demo