[cfe-dev] Clang (3.1 and 3.2): __attribute__((overloadable)) and enum types.
Lizunov, Andrey E
andrey.e.lizunov at intel.com
Fri Aug 2 07:02:39 PDT 2013
Thank you for the answer,
So, it is not a bug but a standard behavior in C unlike of C++.
To make it work a variable of enum type should be defined.
$ cat try.c
#include <stdio.h>
typedef enum {ONE = 1} One;
typedef enum {TWO = 2} Two;
__attribute__((overloadable)) void foo(One arg){
printf("One = %d\n", arg);
}
__attribute__((overloadable)) void foo(Two arg){
printf("Two = %d\n", arg);
}
int main() {
One one = ONE;
Two two = TWO;
foo(one);
foo(two);
return 0;
}
bash-4.1$ clang -x c try.c; ./a.out
One = 1
Two = 2
BR,
Andrey Lizunov
From: Ólafur Waage [mailto:olafurw at gmail.com]
Sent: Friday, August 02, 2013 5:02 PM
To: Lizunov, Andrey E
Cc: cfe-dev at cs.uiuc.edu
Subject: Re: [cfe-dev] Clang (3.1 and 3.2): __attribute__((overloadable)) and enum types.
// bleh.cpp
#include <iostream>
enum class One : int {DUMMY_1 = 1};
enum class Two : unsigned int {DUMMY_2 = 1};
enum class Three : char {DUMMY_3 = 1};
void foo(One)
{
std::cout << "One" << std::endl;
}
void foo(Two)
{
std::cout << "Two" << std::endl;
}
void foo(Three)
{
std::cout << "Three" << std::endl;
}
int main()
{
foo(One::DUMMY_1);
foo(Two::DUMMY_2);
foo(Three::DUMMY_3);
return 0;
}
//
$ clang++ -std=c++11 bleh.cpp -o bleh
$ ./bleh
One
Two
Three
On Fri, Aug 2, 2013 at 12:57 PM, Ólafur Waage <olafurw at gmail.com<mailto:olafurw at gmail.com>> wrote:
Since enums in C are just named integers, they are all of the same type. So it would be the same as doing three functions with int as the argument.
On Fri, Aug 2, 2013 at 12:29 PM, Lizunov, Andrey E <andrey.e.lizunov at intel.com<mailto:andrey.e.lizunov at intel.com>> wrote:
Hello,
I was trying to use __attribute__((overloadable)) for functions accepting enumerated types but hadn’t succeeded so far.
Clang 3.1 and 3.2 failed to compile the following example.
/* clang -x c foo.cpp */
typedef enum __One {DUMMY_1} One;
typedef enum {DUMMY_2} Two;
enum Three {DUMMY_3};
__attribute__((overloadable)) void foo(One);
__attribute__((overloadable)) void foo(Two);
__attribute__((overloadable)) void foo(enum Three);
int main()
{
foo(DUMMY_1);
return 0;
}
Providing me with the following error message:
$ clang -x c foo.cpp
foo.cpp:22:3: error: call to 'foo' is ambiguous
foo(DUMMY_1);
^~~
foo.cpp:16:36: note: candidate function
__attribute__((overloadable)) void foo(One);
^
foo.cpp:17:36: note: candidate function
__attribute__((overloadable)) void foo(Two);
^
foo.cpp:18:36: note: candidate function
__attribute__((overloadable)) void foo(enum Three);
^
1 error generated.
While with “-x c++” both versions of Clang compiled it successfully (failed to link though).
According to http://clang.llvm.org/docs/LanguageExtensions.html#function-overloading-in-c overloadable attribute is an analogue for C++ function overloading in C.
So, is it a bug in the Clang or I didn’t understand/consider something?
BR,
Andrey Lizunov
--------------------------------------------------------------------
Closed Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
_______________________________________________
cfe-dev mailing list
cfe-dev at cs.uiuc.edu<mailto:cfe-dev at cs.uiuc.edu>
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
--------------------------------------------------------------------
Closed Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130802/d7322781/attachment.html>
More information about the cfe-dev
mailing list