이 C++ 문을 어떻게 해석해야 할까요?

최종호의 이미지

int
main(int argc, char **argv)
{
  vector<int> vec;
  vec.push_back(3);
  vec.push_back(1);
  vec.push_back(2);
 
  sort(vec.begin(), vec.end(), greater<int>());
}

greater<int>() 부분을 어떻게 이해해야 할지 모르겠습니다.
심정적으로는 greater<int> 의 function object가 들어가야 할 부분이므로 greater<int>의 temporary object를 생성하는 것으로
생각이 되는데,
파라메터를 받는 constructor 가 있는 경우에는
SomeFunctionClass(1, 3, 4) 와 같은 식으로 해 주는데, 없는 경우에는 왜 괄호를 붙여주는 것일까요?
변수 정의 방식과 연결시켜서 생각해 보려고 해도 잘 안되네요.

SomeFunctionClass sfc(1, 3, 4);
OtherFunctionClassWithNoParam ofc;

이걸 그대로 병치시켜보면
sort(vec.begin(), vec.end(), SomeFunctionClass(1, 3, 4));
sort(vec.begin(), vec.end(), OtherFunctionClassWithNoParam);

와 같이 하는 것이 맞을 것 같으면서도, 또 한편 생각해 보면
함수 호출시에 아래처럼 오브젝트가 아닌 타입을 넘기는 것과 비슷한 느낌이라서 안 맞는 것 같기도 합니다.
f(1, 4, OtherFunctionClassWithNoParam);

추가질문으로, 위 소스를

sort(vec.begin(), vec.end(), greater&lt;int&gt;);

와 같이 수정해서 컴파일을 하면 <int> 다음의 괄호의 위치에서
 g++ q.cc
q.cc: In function 'int main(int, char**)':
q.cc:18: error: expected primary-expression before ')' token

와 같은 오류가 발생합니다. 이게 g++ 이 에러때문에 바보가 되어서 에러 원인을 잘못 파악한 것일까요?
아니면 정말 뭔가 알고서 낸 에러메시지일까요?

klara의 이미지

생성자호출입니다.
생성자도 함수이고, 인자를 받지 않더라도 ()가 있어야합니다.
말씀하신대로 ()가 없다면 그냥 타입을 넘겨주는 게 되버리구요.
OtherFunctionClassWithNoParam ofc;
여기서 ofc를 선언할때 괄호를 안치는 걸로 이상하다고 말씀하시는 것 같은데, 저렇게 선언을 하면 자동으로 컴파일시에 OtherFunctionClassWithNoParam()을 호출합니다.
만약에 생성자에 OtherFunctionClassWithNoParam()가 없다면 컴파일에러가 발생하구요.
OtherFunctionClassWithNoParam ofc(); 와 동일합니다.

최종호의 이미지


네, 역시 생성자호출이었네요.

OtherFunctionClassWithNoParam ofc(); 은 default constructor를 이용해서 OtherFunctionClassWithNoParam 객체를 생성하는 것이 아니라
input parameter가 지정되지 않고 OtherFunctionClassWithNoParam 를 리턴하는 함수 ofc 를 선언하는 것으로 알고 있습니다.

추가 질문....
전자로 해석되지 않고 후자로 해석되게 하는 문법상의 규칙이 어떤건가요?

lifthrasiir의 이미지

해당 상황에서 해석이 모호한 경우 항상 함수 정의로 해석하는 규칙이 있습니다. 정확히 표준 어디에 있는지는 잘 모르겠군요.

댓글 달기

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