![](/img/trans.png)
[英]Templated derived class in CRTP (Curiously Recurring Template Pattern)
[英]Curiously recurring template pattern (CRTP) and derived constructor arguments
我使用奇怪的重復模板模式通過以下方式創建共享指針(如下)。 在Derived :: create(...)上,Visual Studio IntelliSense顯示的可用參數是(Args && ... args)。 如何將派生類的構造函數參數列表傳遞給Base,以便IntelliSense告訴我可用的參數是(const std :: string&str,int i)?
#include <memory>
#include <string>
template<typename T>
class Base
{
public:
template<typename... Args >
static std::shared_ptr<T> create(Args&&... args)
{
return std::make_shared<T>(std::forward<Args>(args)...);
}
};
class Derived : public Base<Derived>
{
public:
Derived(const std::string &str, int i) {}
};
int main()
{
auto derived = Derived::create("text", 123);
}
“如何將Derived類構造函數參數列表傳遞給Base,以便IntelliSense告訴我可用的參數是(const std :: string&str,int i)?”
#include <string>
#include <memory>
template<typename T>
class Base {
public:
template<typename... Args >
static std::shared_ptr<T> create(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
};
class Derived : public Base<Derived> {
public:
Derived(const std::string &str, int i) {}
};
int main() {
auto derived = Derived::create("text", 123);
}
好吧,您的代碼可以正常編譯 。
任何IDE的Intellisense功能始終與用於它們的c ++解析器一樣好。 這完全取決於實際使用的IDE,並且您不應該針對IDE的功能來設計自己的設計,而是可以編譯並很好地進行工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.