Why doesn't this simple enable_if work?

quant

I'm having a problem with an enable_if construct which I've managed to reduce to a very simple piece of failing code:

template <typename Enable, typename...Args>                                 
struct Get;                                                     

template <typename FirstArg, typename... OtherArgs>                       
struct Get<typename std::enable_if<true>::type, FirstArg, OtherArgs...>
{                                                                               
    using type = FirstArg;                                                          
};                                                                              

(Live example)

The above code is a gutted version of some code that did something useful, but for the sake of this question I'm more interested in why this doesn't work, not whether or not it's ideal or does anything useful. The meta function Get should take the first type passed to it and, based on some condition (in this case always true as I didn't construct a case for when it is not) return it.

I don't want the first argument to resolve to the enable_if condition, that should be completely abstracted.

When I try to run this (see live example) it produces

error: ‘type’ in ‘struct Get’ does not name a type using T = typename Get::type

Why?

Yakk - Adam Nevraumont

The specialization you wrote only kicks in if someone passed void as the first argument to Get.

In your example code, you passed int. So the specialization does not apply.

The names of the types in the template definition and specialization have no connection, just their position in the Get<blah, blah, blah>. And it is pattern matching off the primary definition.

int main() {
  using T = Get<void, int>::type;
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why isEOF doesn't work?

분류에서Dev

Why json doesn´t work?

분류에서Dev

Why doesn't isdigit() work?

분류에서Dev

htaccess simple rewrite rule doesn't work

분류에서Dev

Why doesn't LibreOffice work with the HUD?

분류에서Dev

Why wmctrl doesn't work for certain windows?

분류에서Dev

Why doesn't this math work with macros?

분류에서Dev

Why doesn't beep work in Ubuntu?

분류에서Dev

Why doesn't my SetLength work?

분류에서Dev

why the click() function doesn't work?

분류에서Dev

Why doesn't this Mongoose query work?

분류에서Dev

Why doesn't my iptables rule work?

분류에서Dev

Why this jQuery doesn't work in Codeigniter?

분류에서Dev

Why doesn't source lib/* work?

분류에서Dev

Jquery Why doesn't toggleClass work here?

분류에서Dev

Why char[] work,but char * doesn't

분류에서Dev

why doesn't history work in Ubuntu?

분류에서Dev

Why doesn't this batch command work as expected?

분류에서Dev

Why doesn't "usermod" work under superuser?

분류에서Dev

Why doesn't my .desktop file work?

분류에서Dev

Simple WoW Interface Addon doesn't work (Lua)

분류에서Dev

BASH - Simple script with rm command doesn't work

분류에서Dev

Why simple console.log() doesn't works with while?

분류에서Dev

Why doesn't Vodafone Mobile broadband work in UK?

분류에서Dev

Why switch case in javascript doesn't work with string on Microsoft Edge?

분류에서Dev

Why APIPA doesn't work with multiple network adapters?

분류에서Dev

Why APIPA doesn't work with multiple network adapters?

분류에서Dev

Why doesn't Bash completion for 'cc' and 'c++' work?

분류에서Dev

Why doesn't --type-set work in ack?