![](/img/trans.png)
[英]Is substitution failure an error with dependent non-type template parameters?
[英]Dependent Non-Type Template Parameters
考慮以下課程:
class Foo
{
enum Flags {Bar, Baz, Bax};
template<Flags, class = void> struct Internal;
template<class unused> struct Internal<Bar, unused> {/* ... */};
template<class unused> struct Internal<Baz, unused> {/* ... */};
template<class unused> struct Internal<Bax, unused> {/* ... */};
};
在VC ++ 2010和Comeau C ++上測試時,上面的類概述按預期編譯和運行。 但是,當Foo
本身成為模板時,上面的代碼片段在VC ++ 2010下會中斷。
例如,以下代碼段:
template<class> class Foo
{
// Same contents as the original non-templated Foo.
};
產生以下錯誤類 :
C2754: 'Foo<<unnamed-symbol>>::Internal<Bar,unused>' : a partial specialization cannot have a dependent non-type template parameter
C2754: 'Foo<<unnamed-symbol>>::Internal<Baz,unused>' : a partial specialization cannot have a dependent non-type template parameter
C2754: 'Foo<<unnamed-symbol>>::Internal<Bax,unused>' : a partial specialization cannot have a dependent non-type template parameter
Foo
保留內部偽顯式特化)? 如何在VC ++ 2010上修復此問題(即,在模板化的Foo中保留內部偽顯式特化)?
您可以通過在非模板基類中聲明它來使枚舉類型非依賴(C ++ 03使嵌套類依賴於#108但不包括枚舉,但即使這樣的代碼仍然合法) 。
struct FooBase {
enum Flags {Bar, Baz, Bax};
};
template<class> class Foo : public FooBase {
template< ::FooBase::Flags, class = void > struct Internal;
// same other stuff ...
};
“錯誤類”鏈接已經給出了應該提高錯誤的預期情況的描述。 該錯誤認為所有依賴類型都是被禁止的,但實際上這就是標准所說的:
與專用非類型參數對應的模板參數的類型不應取決於特化的參數。
因此,即使Flags
這個名稱在某種程度上依賴,只要它不依賴於專業化的參數(如“錯誤類”鏈接的示例),就不會使其形成錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.