🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by Maciek Wed, 14 Sep 2022 09:34
variadic templates - rozwijanie argumentów, błąd kompilacji
#291888
Author: Maciek
Date: Wed, 14 Sep 2022 09:34
43 lines
1051 bytes
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 ?
Re: variadic templates - rozwijanie argumentów, błąd kompilacji
#291889
Author: 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).
Re: variadic templates - rozwijanie argumentów, błąd kompilacji
#291890
Author: =?UTF-8?Q?Wojcie
Date: Sat, 17 Sep 2022 13:43
36 lines
1081 bytes
On Wednesday, September 14, 2022 at 6:34:23 PM UTC+2, Maciek wrote:
> kompilator nie chce rozwinąć funkcji i nie pasuje mu liczba argumentów 

Nie może rozwinąć funkcji, bo ona jest w jednym wariancie. Parametryzację
masz na poziomie klasy, nie metody.

> Na czym polega mój błąd ?

Tak pi*drzwi sygnatura tej jednej metody to (int, int, int , int),
bo T = int, Types = {int, int, int}.

Więc to co się teraz dzieje w Twoim kodzie:

T_Operators_tests<int,int,int,int>::print(int val, {int, int, int} args)
{
    std::cout << val;
    print(args[0], args[1], args[2]);
}

Nie masz *w klasie* metody print(int, int, int) i to właśnie napisał Ci kompilator:
"no overloaded function takes 3 arguments".

Rozwiązanie to niezależnie od klasy sparametryzować metodę `print`:

template <typename U, typename... TU>
void print(U val, TU... args) {
    // ...
}

BTW, doczytaj o perfect forwarding, bo przekazywanie
wartości w C++ to proszenie się o kłopoty.

w.
Re: variadic templates - rozwijanie argumentów, błąd kompilacji
#291911
Author: Przemek Biernat
Date: Wed, 29 Mar 2023 07:41
47 lines
1387 bytes
On Wednesday, September 14, 2022 at 6:34:23 PM UTC+2, Maciek wrote:
> 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 ?

Przestańcie używać variatic templetes, bo to już jest zboczenie.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads