2017-02-13 20 views
7

Bu koda sahibim, beklenti, şablon parametresinin türüne göre operatörün iki farklı sürümü olan () olacaktır. Neden enable_if burada çalışmıyor?

#include <string> 
#include <type_traits> 

template<typename T> 
struct Impl 
{ 
    std::enable_if_t<!std::is_pointer<T>::value,T> operator()(const std::string& key, int node) 
    { 
     return static_cast<T>(); 
    } 
    std::enable_if_t<std::is_pointer<T>::value,T> operator()(const std::string& key, int node) 
    { 
     return new T(); 
    } 
}; 

int main() 
{ 
} 

Onun yerine bir hata derleme olsun: 'std::enable_if_t<std::is_pointer<_Tp>::value, T> Impl<T>::operator()(const string&, int)' cannot be overloaded with 'std::enable_if_t<(! std::is_pointer<_Tp>::value), T> Impl<T>::operator()(const string&, int)'

+1

Nit Çekme: nedir? Static_cast (); '? – WhiZTiM

+1

@WhiZTiM [ftfy] (http://coliru.stacked-crooked.com/a/4418f30d119f86fe) –

cevap

10

Sizin operator() fonksiyon kendilerini şablonları değildir, bu yüzden SFINAE için bir bağlam yoktur. Bunu deneyin:

template <typename U = T> 
std::enable_if_t<!std::is_pointer<U>::value,U> operator()(const std::string& key, int node) 
{ 
    return static_cast<U>(); 
} 

template <typename U = T> 
std::enable_if_t<std::is_pointer<U>::value,U> operator()(const std::string& key, int node) 
{ 
    return new U(); 
} 
+5

Cevabınız doğruyken. static_cast (); 'geçersiz bir ifade – WhiZTiM

+1

Yanıtınız nedir? – NeomerArcana

+1

@NeomerArcana Şablondakilerle aynı struct Impl {...}; ' – YSC