2015-05-15 15 views
9

Utilizzando clang 3.6.0, non riesco a compilare il seguente esempio di codice.È possibile utilizzare una variabile di template `constexpr` come predefinita per un argomento di template formale

#include <type_traits> 

template <typename T> constexpr bool IS_SCALAR = ::std::is_scalar<T>::value; 
template <typename T, bool = IS_SCALAR<T>> 
struct Class_Breaks 
{ 
}; 

template <typename T, bool = ::std::is_scalar<T>::value> 
struct Class_Works 
{ 
}; 

void function() 
{ 
    Class_Breaks<int> break_error; 
    Class_Breaks<int, IS_SCALAR<int>> breaks_ok; 
    Class_Works<int> ok; 
} 

Ma, i seguenti messaggi di errore vengono restituiti:

1> [ 66%] Building CXX object CMakeFiles/Core.dir/tests.cpp.obj 
1>D:\Projects\Core\Core\tests.cpp(4,30): error : non-type template argument is not a constant expression 
1> template <typename T, bool = IS_SCALAR<T>> 
1>        ^
1> D:\Projects\Core\Core\tests.cpp(16,18) : note: while checking a default template argument used here 
1>   Class_Breaks<int> break_error; 
1>   ~~~~~~~~~~~~~~~~^ 
1> 1 error generated. 
+4

È un [bug noto] (https://stackoverflow.com/questions/10721130/) – StenSoft

risposta

4

Come menzionato da @StenSoft, è un known bug. Se avete bisogno di fare è lavorare perché si ha una variabile constexpr modello che si desidera utilizzare come predefinito, si può avvolgere il valore di default in un std::intergral_constant:

template< 
    typename T, 
    bool = std::integral_constant< bool, IS_SCALAR<T> >::value 
> 

Live example

0

Questo non è fisso in clang 3.7. Il rapporto sui bug a cui fa riferimento Daniel Frey si riferisce alle funzioni constexpr (che funzionano ora) ma non ai modelli variabili.

Problemi correlati