함수의 인수를 반환하는 기능은 없나요?

dltkddyd의 이미지

int funfun(int (*fp)(void)) {
	cout<<"funfun"<<endl;
	return 0;
}
int fun(double, float) {
	return 0;
}
template<typename ExtractedType=int, typename... Args>
class IsFun {
public:
	bool isReallyFun;
	/*isFun<ExtractedType, Args...>() {
		isReallyFun=false;
	}*/
	IsFun( ExtractedType (*fp)(Args... args) ) {
		isReallyFun=true;
	}
};
 
int main() {
	IsFun<int,double,float> obj2(fun);
	cout<<"obj2.isReallyFun is "<<obj2.isReallyFun<<endl;
 
	/*
	IsFun의 <>안에 함수를 넣으면 자동으로 그 함수의 형을 컴파일 전에 언급해주는 기능은 없나요?
	저 위의 예에서 fun  함수는 포인터 식으로 표현하자면 다음고 같은 꼴이죠
 
	int (*fp)(double,float)
 
	이런 식의 함수이름이 fun인데 이 함수이름을 전달하면, 그 반환타입과 형식매개변수의 타입을 반환해주는 기능말입니다.  fun을 전달했을 때, 결과적으로 int, double, float를 언급해줌으로써 저 main 함수 안의 IsFun <> 안에 int, double, float가 언급될 수 있도록 말입니다.
	*/
	return 0;
}

어떻게 하다 보니 질문을 코드에 넣어버렸습니다. 질문드린 문제에 대한 해답은 없을까요?

kukyakya의 이미지

컴파일러 확장을 이용하시거나 C++11의 decltype을 사용하면 가능합니다.

컴파일러 확장의 경우 boost::typeof(http://www.boost.org/doc/libs/release/doc/html/typeof.html)를 사용하시면 메이저 컴파일러에서 지원하는 확장기능을 이용하여 작성한 TYPEOF 매크로를 사용할 수 있습니다.

klyx의 이미지

'언급하다'가 어떤 행동인지 정의해주세요. 타입을 보여주는 에러메시지를 뿜어야된다는 뜻인가요?

dltkddyd의 이미지

IsFun obj1;

이라고 하거나 또는
IsFun obj1;

이라고 했을 때, fun 함수의 반환타입과 그 매개변수들을 컴마로 구분하여 언급한다라는 뜻이었습니다. 그렇게 할 수 있을까요?

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

klyx의 이미지

kaeri17의 이미지

여기다가 typeid 같이 쓰면 충분히 런타임에 함수의 인자를 출력 할 수 있을것 같네요. 컴파일 타임에 출력하고 싶으면 typeid의 템플릿 버전을 만들어야 할겁니다. 템플릿 메타프로그래밍 지식이 조금만 있으시면 compile time string과 static_assert를 통해 충분히 구현 가능합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.