对于其他类型,是否存在void_t的标准泛化?

时间:2017-06-23 07:41:02

标签: c++ sfinae c++17

在C ++ 17中,我们有std::void_t,这使得SFINAE看起来更好:

template <typename T>
std::void_t<decltype(T::prop)> foo() { /* stuff */ }

仅当T::prop存在时,模板功能才会存在。

如果T::prop存在,则模板函数foo()将等同于:

template <typename T>
void foo() { /* stuff */ }

否则,代码相当于根本不声明foo()

标准库中的其他类型是否有std::void_t的概括,如下所示:

template<typename T, typename...>
using generic_t = T;

以便下面的代码有效吗?

template <typename T>
std::generic_t<int, decltype(T::prop)> foo() { /* stuff */ }

这相当于

template <typename T>
int foo() { /* stuff */ }

如果T::prop存在?

2 个答案:

答案 0 :(得分:5)

为什么需要这样的推广? >>> from selenium import webdriver >>> driver = webdriver.Chrome() >>> driver.get('https://www.parlament.ch/de/ratsbetrieb/suche-curia-vista') >>> driver.find_element_by_id('PageLinkNext').click() >>> DOM = driver.execute_script('return document.getElementById("ng-app").innerHTML;') >>> page = bs4.BeautifulSoup(DOM, 'lxml') >>> page.find_all('h4', {'class', "ms-srch-item-area"}) [<h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173611">Interpellation - Herzog Verena</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173610">Interpellation - Tornare Manuel</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173609">Postulat - Gmür Alois</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173608">Interpellation - Reynard Mathias</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173607">Motion - FDP-Liberale Fraktion</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173606">Interpellation - Bourgeois Jacques</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173605">Motion - Gmür-Schönenberger Andrea</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173604">Motion - Fraktion BD</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173603">Postulat - Dettling Marcel</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173602">Postulat - Mazzone Lisa</a> </h4>] >>> driver.quit() >>> from selenium import webdriver >>> driver = webdriver.Chrome() >>> driver.get('https://www.parlament.ch/de/ratsbetrieb/suche-curia-vista') >>> driver.find_element_by_id('PageLinkNext').click() >>> DOM = driver.execute_script('return document.getElementById("ng-app").innerHTML;') >>> import bs4 >>> page = bs4.BeautifulSoup(DOM, 'lxml') >>> page.find_all('h4', {'class', "ms-srch-item-area"}) [<h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173611">Interpellation - Herzog Verena</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173610">Interpellation - Tornare Manuel</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173609">Postulat - Gmür Alois</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173608">Interpellation - Reynard Mathias</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173607">Motion - FDP-Liberale Fraktion</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173606">Interpellation - Bourgeois Jacques</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173605">Motion - Gmür-Schönenberger Andrea</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173604">Motion - Fraktion BD</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173603">Postulat - Dettling Marcel</a> </h4>, <h4 class="ms-srch-item-area"> <a href="/de/ratsbetrieb/suche-curia-vista/geschaeft?AffairId=20173602">Postulat - Mazzone Lisa</a> </h4>] 有点特别之处在于它可以帮助您轻松编写类型特征,因为您可以将某个类型的主要版本默认为void_t,使用void进行专门化。例如:

void_t

并不是template <class T, class = void> struct has_prop : std::false_type { }; template <class T> struct has_prop<T, std::void_t<decltype(T::prop)>> : std::true_type { }; 有什么特别之处,你只需要在主要和专业化之间达成一致意见。

如果您直接在SFINAE中使用它,那么

void没有多大意义。你可以把表达式粘贴在其他地方:

void_t

此时返回类型与您要检查的条件完全分开,因此如果您需要template <typename T, class = decltype(T::prop)> void foo() { /* stuff */ }

int

答案 1 :(得分:1)

它可能不存在。它没有在文档中链接,因此我怀疑它的存在。但是你可以自己构建这样的类型:

CREATE OR REPLACE FUNCTION edm.automated_builder_update_trigger_manual()
RETURNS trigger AS
$BODY$
DECLARE
  e record;
  weekly_permit_table text := TG_ARGV[0];
BEGIN
  EXECUTE 
    format(
      $q$
        UPDATE edm.%I AS a SET
          applicant = concat(applicant,' - '||$1)
        WHERE 
          $2 = split_part(a.permit_details, ' ',1)$q$,
      weekly_permit_table)
    USING NEW.builder, OLD.permit_number;
  RETURN NULL;
END $BODY$;