2014-06-21 12 views
10

Rozważmy następujący przykład:zwężenie int BOOL SFINAE, różnych mocy od gcc i brzękiem

template<int i> 
struct nice_type; 

template<class T> 
struct is_nice : std::false_type {}; 

template<int i> 
struct is_nice< nice_type<i> > : std::integral_constant<int, i> {}; 

template<class T, class = void> 
struct pick 
{ 
    typedef std::integral_constant<int, -1> type; 
}; 

template<class T> 
struct pick<T, typename std::enable_if< is_nice<T>::value >::type > 
{ 
    typedef std::integral_constant<int, is_nice<T>::value > type; 
}; 

int main() 
{ 
    std::cout << pick<int>::type::value << ", "; 
    std::cout << pick< nice_type<42> >::type::value << std::endl; 
    return 0; 
} 

szczęk (3.4.1) wyjścia "-1 -1", natomiast GCC (4.9.0) wyjścia "-1, 42".

Problem leży w specjalizacji pick. Podczas gdy Gcc wydaje się być szczęśliwy z konwersji is_nice<T>::value (42) na bool(true), clang tego nie robi i odrzuca specjalizację. Oba przykłady zostały skompilowane z -std=c++11.

Który kompilator ma rację?

+1

Czy chodziło Ci 'nice_type' zamiast' cool_type' w wierszu 8? –

+0

@RSahu tak, przepraszam. – sbabbi

+0

Po co to? –

Odpowiedz

9

To jest błąd gcc 57891. Konwersja stałej całkowej 42 do bool obejmuje zwężającą się konwersję, która nie jest dozwolona w przypadku argumentów szablonu non-type. W związku z tym enable_if jest źle sformułowany, a specjalizacja pick powinna zostać odrzucona, ponieważ poprawnie działa clang.

§14.3.2/5 [temp.arg.nontype]

The following conversions are performed on each expression used as a non-type template-argument. If a non-type template-argument cannot be converted to the type of the corresponding template-parameter then the program is ill-formed.
— For a non-type template-parameter of integral or enumeration type, conversions permitted in a converted constant expression (5.19) are applied.
...

§5.19/3 [expr.const]

... A converted constant expression of type T is an expression, implicitly converted to a prvalue of type T , where the converted expression is a core constant expression and the implicit conversion sequence contains only user-defined conversions, lvalue-to-rvalue conversions (4.1), integral promotions (4.5), and integral conversions (4.7) other than narrowing conversions (8.5.4).

§8.5.4/7 [dcl.init.list]

A narrowing conversion is an implicit conversion
...
— from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.


Ten minimal example demonstruje gcc błąd:

template<bool> 
struct foo{}; 
foo<10> f; 

int main() {} 

gcc-4.9 akceptuje kodu podczas dzyń-3,4 odrzuca je z powodu następującego błędu:

error: non-type template argument evaluates to 10, which cannot be narrowed to type 'bool' [-Wc++11-narrowing]

foo<10> f; 
    ^

poprawkę do danego problem jest łatwy. Upewnij się, że szablon typu non-argument enable_if ocenia na bool

template<class T> 
struct pick<T, typename std::enable_if< is_nice<T>::value != 0 >::type > 
//              ^^^^^^ 
{ 
    typedef std::integral_constant<int, is_nice<T>::value > type; 
}; 
Powiązane problemy