我有這樣的結構:
struct i32 {
template<int32_t x>
struct val {
static constexpr int32_t v = x;
};
template<typename v1, typename v2>
struct add {
using type = val<v1::v v2::v>;
};
template<typename v1, typename v2>
using add_t = typename add<v1, v2>::type;
};
顯然,我在該結構中有更多方法和嵌套型別。而且我還有其他類似的結構,例如i64等等,我將其命名為“Rings”。
稍后在代碼中,我有一個模板結構,
template<typename Ring>
struct FractionField {
// many things here
};
我想為我的 Ring 型別提供一個 c 概念,因此我可以在編譯型別中檢查用戶定義的“Rings”是否具有適當的嵌套型別和操作。
我的嘗試看起來像這樣:
template<typename T>
concept RingConcept = requires {
typename T::val; // how to express that val must be a template on a numeric value ??
typename T::template add_t<typename T::val, typename T::val>; // same here ??
};
但是,我的任何嘗試都沒有定論。
基本上,我總是遇到這種錯誤,但不知道如何解決它們:
error: constraints not satisfied for alias template 'FractionField' [with Ring = i32]
using Q32 = FractionField<i32>;
note: because 'typename T::template add_t<typename T::val, typename T::val>' would be invalid: typename specifier refers to class template member in 'i32'; argument deduction not allowed here
typename T::template add_t<typename T::val, typename T::val>;
我希望我不需要為用于嵌套模板的每種數字型別撰寫一個概念 val。
uj5u.com熱心網友回復:
-子句中typename的requires后面是型別而不是模板。由于T::val是一個接受數值的類模板,因此只需實體化T::val就0足夠了
template<typename T, typename val = typename T::template val<0>>
concept RingConcept = requires {
typename T::template add_t<val, val>;
};
或者
template<typename T>
concept RingConcept = requires {
typename T::template val<0>;
typename T::template add_t<typename T::template val<0>,
typename T::template val<0>>;
};
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/491229.html
上一篇:模糊類模板轉換
