🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.c
Article #291889

Re: variadic templates - rozwijanie argumentów, błąd kompilacji

#291889
From: Maciek Godek
Date: Fri, 16 Sep 2022 16:09
59 lines
1860 bytes
środa, 14 września 2022 o 18:34:23 UTC+2 Maciek napisał(a):
> kod poniższy działa: 
> 
> void print() { 
> std::cout << "IN FUNCTION " << __func__ << std::endl; 
> std::cout << "ENDING ..." << std::endl; 
> return; 
> } 
> 
> template<typename T, typename ... A> 
> auto print(T a, A... args) -> void { 
> std::cout << "IN FUNCTION " << __func__ << std::endl; 
> std::cout << a << std::endl; 
> print(args ...); 
> } 
> 
> Natomiast w przypadku szablonu: 
> 
> template<typename T, typename ... Types> 
> class T_Operators_tests { 
> public: 
> T_Operators_tests() {} 
> ~T_Operators_tests() {} 
> 
> void print() { 
> std::cout << "IN FUNCTION " << __func__ << std::endl; 
> std::cout << "ENDING ..." << std::endl; 
> return; 
> } 
> void print(T var1, Types... var2) 
> { 
> std::cout << var1 << std::endl; 
> 
> this->print(var2...); 
> } 
> }; 
> 
> kompilator nie chce rozwinąć funkcji i nie pasuje mu liczba argumentów 
> 
> Severity Code Description Project File Line Suppression State 
> Error C2661 'T_Operators_tests<int,int,int,int>::print': no overloaded function takes 3 arguments TestCpp1 operators_learn.hpp 37 
> 
> Na czym polega mój błąd ?

Cóż, nie za bardzo rozumiem C++, ale jeśli bym miał zgadywać,
to wygląda mi na to, że problem jest taki, że w tym drugim przypadku
każde ukonkretnienie szablonu powoduje powstanie nowej klasy,
w której metoda print przyjmuje konkretną liczbę argumentów.

Natomiast w ciele metody "print" próbujesz użyć (poprzez this)
metodę print, która będzie raczej przynależeć do innej klasy
(tzn. ukonkretnionej dla mniejszej liczby argumentów).

Message-ID: <b18725ba-4286-43f5-b72f-5ecfa09d8908n@googlegroups.com>
Path: polish.pugleaf.net!archive.newsdeef.eu!archive!apf8.newsdeef.eu!nnrp.usenet.blueworldhosting.com!!spool1.usenet.blueworldhosting.com!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
References: <28531d26-4cfe-4805-99ba-96dcbf0e18e4n@googlegroups.com>