c – 检查模板是否具有给定类型的特化

c – 检查模板是否具有给定类型的特化,第1张

概述假设我们有一个这样的元函数: template<typename LHS , typename RHS>struct add; 我们有一组已知类型的专业化.例如,整体包装: template<typename T1 , T1 VALUE1 , typename T2 , T2 VALUE2>struct add<std::integral_constant<T1,VALUE1>,std::in 假设我们有一个这样的元函数:

template<typename LHS,typename RHS>struct add;

我们有一组已知类型的专业化.例如,整体包装:

template<typename T1,T1 VALUE1,typename T2,T2 VALUE2>struct add<std::integral_constant<T1,VALUE1>,std::integral_constant<T2,VALUE2> : public std::integral_constant<std::common_type_t<T1,T2>,VALUE1+VALUE2> {}; //Take advantage of C++14 helpers

几天前,我们创建了一个新类,我们专门为这个类添加了:

template<typename X,typename Y>struct point_2d{    using x = X;    using y = Y;};template<typename X1,typename Y1,typename X2,typename Y2>struct add<point_2d<X1,Y1>,ppoint_2d<X2,Y2>> : public point_2d<add<X1,X2>,add<Y1,Y2>> {};

如您所见,我使用add Metafuntion来执行坐标的添加.因此,任何具有添加元函数专门化的类型都可以用作point_2d坐标.

我的问题是:有没有办法检查模板是否具有给定类型作为参数的特化

像这样的东西:

template<template<typename...> class TEMPLATE,typename... Ts>struct has_specialization;template<template<typename...> class TEMPLATE,typename... Ts>struct has_specialization<TEMPLATE<Ts...> /* TEMPLATE<Ts...> exists */,Ts...> : public std::true_type {};template<template<typename...> class TEMPLATE,typename... Ts>struct has_specialization<TEMPLATE<Ts...> /* TEMPLATE<Ts...> doesn't exists */,Ts...> : public std::false_type {};
解决方法

@bartekBanachewicz the IDea is to provIDe user frIEndly compiler errors through asserts.
If i can check (Following with the point_2d example) if the coordinates passed are addable
(That is,a specialization of add Metafuncion is or not defined for that coordinates),
I can provIDe more readable errors like “That coordinates are not addable. point_2d needs
addable coordinates for point addition”,instead of the common horrible template instantation errors.

鉴于这是你的问题的动机,这不是一个
更简单直接的解决方案:

#include <type_traits>template<typename LHS,typename RHS>using cannot_add =  std::integral_constant<bool,std::is_same<LHS,LHS>::value>;template<typename LHS,typename RHS>struct add{    /* Assert some expressively named condition that is always false         but is dependent on a template parameter.    */    static_assert(!cannot_add<LHS,RHS>::value,"Types are not addable. Need specialization");};template<typename T1,VALUE2>> : public std::integral_constant<    typename std::common_type<T1,T2>::type,VALUE1+VALUE2> {};  int main(){    add<std::integral_constant<int,1>,std::integral_constant<int,2>> x; // line 25     add<float,float> y; // line 26    return 0;}

GCC 4.8.1诊断:

main.cpp:26:19:   required from heremain.cpp:12:2: error: static assertion Failed: Types are not addable. Need specialization

Clang 3.3诊断:

main.cpp:12:2: error: static_assert Failed "Types are not addable. Need specialization"    static_assert(!cannot_add<LHS,^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~main.cpp:26:19: note: in instantiation of template class 'add<float,float>' requested here    add<float,float> y; // line 26                 ^
总结

以上是内存溢出为你收集整理的c – 检查模板是否具有给定类型的特化全部内容,希望文章能够帮你解决c – 检查模板是否具有给定类型的特化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1215292.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存