🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by Maciek Wed, 14 Sep 2022 16:34
variadic templates - rozwijanie argumentów, błąd kompilacji
#310387
Author: Maciek
Date: Wed, 14 Sep 2022 16:34
42 lines
2354 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
#310388
Author: Maciek Godek
Date: Fri, 16 Sep 2022 23:09
52 lines
3206 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
#310389
Author: Wojciech Muła
Date: Sat, 17 Sep 2022 20:43
33 lines
2545 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
#310390
Author: Przemek Biernat
Date: Wed, 29 Mar 2023 14:41
45 lines
2782 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