typename... Args에서 Args를 반환타입으로 사용할 수 없나요?

dltkddyd의 이미지

임의의 인수를 건내받고자 할 때 typename... Args와 같은 표현을 템플릿의 타입표현에 명시함으로써 임의의 인수를 받아낼 수 있습니다. 그런데 저 Args를 다시 돌려받으려면 어떻게 해야 하나요. 아래와 같은 코드가 있을 때

void C(FirstType first, Args... args) {
	cout<<"In c  "<<first<<endl;
}
 
template<typename FirstType, typename SecondType, typename... Args>
(Args...) b(FirstType first, SecondType second, Args... args) {
	cout<<"In b "<<first<<"  "<<second<<endl;
	return args...;
}
 
template<typename... Args>
void a(Args... args) {
	args=b(args...);//여기서
  	c(args...);
}
 
int main() {
	a(10,20,30,40,50,60,70,80);
 
	return 0;
}

main에서 a함수를 위에서 처럼 사용하면 a 함수의 args는 10...80의 값을 넘겨받습니다. a 함수에서 이를 다시 b 함수를 넘기면서 b함수에서는 값 하나를 떼어내고 나머지 20...80의 값을 ektl 호출부로 돌려주도록 하려고 했는데 args가 값을 제대로 돌려받지 못합니다. 돌려받을 방법이 없을까요?

klara의 이미지

C++에서는 한번에 하나의 객체만 반환할 수 있습니다.

kukyakya의 이미지

std::tuple로 리턴 받으시고 c의 인자로 unpack해서 넘겨주시면 됩니다.

c++ tuple unpack으로 검색해보세요.

dltkddyd의 이미지

말씀하신 방법으로 해결해보려고 다음과 같이 세 가지 경우로 시도해봤습니다. 두 번째 것을 제외하고 컴파일시 모두 오류가 발생합니다. 세 개의 코드는 다음과 같습니다.

#include <iostream>
using namespace std;
#include <tuple>
 
class Test {
public:
	Test() {}
	void a() {}
	template<typename FirstType, typename... Args>
        void a(FirstType first, Args... args) {
		tuple<Args...> obj1=make_tuple(args...);
		cout<<first<<endl;
		cout<< get<0>(obj1) <<endl;//error  message  no maptching function for call to 'get(std::tuple<>&)'
		a(args...);
	}
};
 
int main() {
	Test obj1;
	obj1.a(10,9,8,7,6);
 
	return 0;
}

#include <iostream>
using namespace std;
#include <tuple>
 
class Test {
public:
	Test() {}
	void a() {}
	template<typename FirstType, typename... Args>
	void a(FirstType first, Args... args) {
		tuple<Args...> obj1=make_tuple(args...);
		cout<<first<<endl;
		cout<< get<0>(obj1) <<endl;
		tie(args...)=obj1;//제대로 풀린 것인지?  오류는 나지 않습니다.
	}
};
 
int main() {
	Test obj1;
	obj1.a(10,9,8,7,6);
 
	return 0;
}

class Test {
public:
	Test() {}
	void a() {}
	template<typename FirstType, typename... Args>
	void a(FirstType first, Args... args) {
		tuple<Args...> obj1=make_tuple(args...);
		cout<<first<<endl;
		cout<< get<0>(obj1) <<endl;//error  no matching funtion for call to 'get(std::tuple<>&)'
		tie(args...)=obj1;
		a(args...);
	}
};
 
int main() {
	Test obj1;
	obj1.a(10,9,8,7,6);
 
	return 0;
}

결국 재귀적으로 멤버템플릿함수를 호출할 때에 오류가 발생한다는 것인데, 재귀적으로 호출하면서 오류가 뜨지 않게 하려면 첫 번째와 두 번째의 무엇을 고쳐야 하나요? 그리고 오류가 발생하지 않는 두 번째 것은 tie로 값이 제대로 args에 설정이 된 것인지 궁금합니다.

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

klara의 이미지

재귀를 멈추기위한 특수화를 하셔야죠. 에러메시지에 인자가 없는 tuple에대해서 get을 쓸 수 없다고 잘 나와있네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.