2015년 8월 3일 월요일

C의 오버로딩?! → 이런 지원도 있었나.. 하지만 gcc는 안되는걸.. ≪ _Generic() ≫


#define myfunction(X) _Generic((X), long double:myfunction_longdouble, default:myfunction_double, float:myfunction_float )(X)

"최근에 정의된 C 표준 C11에서는 _Generic()이라는 키워드를 도입해서 컴파일 시점에 이런 기능을 편하게 정의할 수 있도록 하였다. 앞의 코드는 다음과 같이 C 스타일로 변경해서 사용할 수 있다.  ...

(中略)

참고로 이 기능은 C11에서 소개되었기 때문에 이 표준을 따르지 않는 컴파일러에 서는 지원하지 않는다. 실제로 gcc에서 이 코드를 컴파일하면 다음과 같은 에러 메 시지를 출력한다."

- [리얼타임] 프로그래머가 몰랐던 프로그램의 동작 원리(프로그램 개발편) 中..


내용출처: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_13.1.2/com.ibm.xlc131.aix.doc/language_ref/genericselection.html

포럼참고: http://stackoverflow.com/questions/9864513/gcc-allow-overloaded-functions-in-c99

Generic selection (C11)

A generic selection is a primary expression. Its type and value depend on the selected generic association.
The following diagram shows the generic selection syntax:
Read syntax diagramSkip visual syntax diagram
                                          .-,-----------------------------------------.      
                                          V                                           |      
>>-_Generic--(--assignment-expression--,----+-type-name--:--assignment-expression---+-+--)-><
                                            |                                   (1) |        
                                            '-default--:--assignment-expression-----'        


Notes:
  1. A generic selection can have at most one default generic association.
where:
type-name
Specifies the type of a generic association. The type name that you specify in a generic association must be a complete object type other than a variably modified type.
assignment-expression
Is an assignment expression. The first assignment expression is called the controlling expression.
The generic association list is a group of generic associations. There are two forms of generic associations:
  • type-name: assignment-expression
  • default: assignment-expression
One generic selection cannot have two or more generic associations that specify compatible types. In one generic selection, the controlling expression can have at most one compatible type name in the generic association list. If a generic selection has no default generic association, its controlling expression must have exactly one compatible type name in its generic association list.
If there is a generic association with a type name that is compatible with the controlling expression in the generic selection, the expression in the generic selection is the result expression. Otherwise, the result expression of the generic selection is the expression in the default generic association. The controlling expression of a generic selection is not evaluated. None of the expressions from any other generic association of the generic selection is evaluated.
The type and value of a generic selection are identical to those of its result expression. For example, a generic selection is an lvalue, a function designator, or a void expression if its result expression is an lvalue, a function designator, or a void expression.

Example

The following sample myprogram.c defines a type-generic macro:
#define myfunction(X) _Generic((X), \
long double:myfunction_longdouble, \
default:myfunction_double, \
float:myfunction_float \
)(X)
void myfunction_longdouble(long double x){printf("calling %s\n",__func__);}
void myfunction_double(double x){printf("calling %s\n",__func__);} 
void myfunction_float(float x){printf("calling %s\n",__func__);}

int main()
{
  long double ld;
  double d;
  float f;
  myfunction(ld);
  myfunction(d);
  myfunction(f);
} 
When you execute the program:
xlc myprogram.c -qldbl128 -qlanglvl=extc1x
./a.out
the result is as follows:
calling myfunction_longdouble
calling myfunction_double
calling myfunction_float

댓글 없음:

댓글 쓰기