2016-09-05 55 views
2

當我們通過一(多)派生類模板函數期待基類什麼是模板實例規則?例如:模板實例有多個模板繼承

#include <iostream> 

template <int x> 
struct C {}; 

struct D : C<0>, C<1> {}; 

template <int x> 
void f (const C<x> &y) { std::cout << x << "\n"; } 

int main() 
{ 
    f (D()); 
} 

MSVC 2015打印0,鐺3.8 - 1和gcc 6.2給出編譯器錯誤(Demo)。而且,即使你SFINAE-了所有重載除一人外,結果仍然是不同的:

#include <iostream> 

template <int x> struct C {}; 

template<> 
struct C<0> { using type = void; }; 

struct D : C<0>, C<1> {}; 

template <int x, typename = typename C<x>::type> 
void f (const C<x> &y) { std::cout << x << "\n"; } 

int main() 
{ 
    f (D()); 
} 

現在只編譯與MSVC,如果你換C<0>C<1>只鐺會編譯它。問題在於MSVC只嘗試實例化第一個base,clang - last和gcc打印錯誤太早。哪個編譯器是正確的?

+0

在我看來,好像它們都是錯的。它不應該是一個模糊的函數調用嗎? –

+0

*「MSVC打印0,鐺-1和gcc給編譯器錯誤。」*,哪個MSVC,哪個gcc和哪個鐺? –

+0

@PiotrSkotnicki新增版本號,但它們都具有相同的行爲 –

回答

1

GCC 5.4:

/tmp/gcc-explorer-compiler11685-58-1h67lnf/example.cpp: In function 'int main()': 
13 : error: no matching function for call to 'f(D)' 
f (D()); 
^ 
9 : note: candidate: template<int x> void f(const C<x>&) 
void f (const C<x> &y) { std::cout << x << "\n"; } 
^ 
9 : note: template argument deduction/substitution failed: 
13 : note: 'const C<x>' is an ambiguous base class of 'D' 
f (D()); 
^ 
Compilation failed 

這在我看來是正確的結果,因爲C < 0>和C < 1>同樣專業。

海合會6.2

鐺3.8.1同樣的結果編譯它,這在我看來是一個編譯器錯誤。

更新:

我不知道實際的使用情況,但我不知是否會爲你工作:

#include <utility> 
#include <iostream> 

template<class T> 
struct has_type 
{ 
    template<class U> static auto test(U*) -> decltype(typename U::type{}, std::true_type()); 
    static auto test(...) -> decltype(std::false_type()); 
    using type = decltype(test((T*)0)); 
    static const auto value = type::value; 
}; 

template <int x> struct C {}; 

template<> 
struct C<0> { using type = int; }; 

template<int...xs> 
struct enumerates_C : C<xs>... 
{ 
}; 

struct D : enumerates_C<0, 1> {}; 

template<int x, std::enable_if_t<has_type<C<x>>::value>* = nullptr> 
void f_impl(const C<x>& y) 
{ 
    std::cout << x << "\n"; 
} 

template<int x, std::enable_if_t<not has_type<C<x>>::value>* = nullptr> 
void f_impl(const C<x>& y) 
{ 
    // do nothing 
} 

template <int...xs> 
void f (const enumerates_C<xs...> &y) 
{ 
    using expand = int[]; 
    void(expand { 0, 
     (f_impl(static_cast<C<xs> const &>(y)),0)... 
    }); 
} 

int main() 
{ 
    f (D()); 
} 

預期輸出(蘋果鐺測試):

0 
+0

在第一個例子中 - 是的,我認爲它應該是模棱兩可的,第二個 - 不,只應該啓用C <0>的超載。 –

+0

「*僅適用於C <0>應該啓用*」您認爲編譯器應該嘗試使用每個可能的'x'是什麼原因?第一個扣除發生,其中失敗 –

+0

@PiotrSkotnicki我問這個問題,以瞭解它應該如何完成。如果手動寫這個功能,那麼它會工作打算: https://gist.github.com/telishev/a52483833ae6850df69e1e6953f6b277 –