Re: How to extend lanuage C in GCC ?
2008/10/22 Dong Phuong <[EMAIL PROTECTED]>: > > I'm porting for a microcontroller. And now I want to > add some features to the current language C int GCC > (for example some directives such as near, far , > ). SO could you please tell me how I can do that > ? Hi, I am not sure if it is solves all your cases, but things like far, near, that are usually handled as attributes. Please take a look at http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Function-Attributes.html#Function-Attributes. Cheers, Piotr
Variadic template function full specialization.
Hello all, Following (bit weird ;-) code shows weird case of variadic template function specialization, however I am not sure it is legal, (atleast I haven't found any wording that would prevent such use): enum SelectFoo {S1, S2, S3}; struct A { template void foo (Args_...); }; template <> void A::foo (int) {} // #1 compiles fine template <> void A::foo () {} // #2 this won't compile (error: template-id 'foo<(SelectFoo)1u>' for 'void A::foo()' does not match any template declaration) template <> void A::foo (int, int) {} // #3 this won't compile too (error: template-id 'foo<(SelectFoo)2u>' for 'void A::foo(int, int)' does not match any template declaration) I have checked this code with g++-4.3.2 and g++-4.4.0-alpha20081010 (gentoo ebuild) It seems that variadic type pack Args_... is always treated as one parameter. When I have tried declaration like: template void foo (T_, More_...); it could match specialization with two parameters only, 'foo(int, int)' in this example. If I would provide all three declarations ie. struct A { template void foo (); template void foo (T_); template void foo (T1_, T2_); }; everything compiles fine, with both gcc and Comeau C/C++ online, as one would expect. My question is: whether all specializations (#1, #2, #3) should be accept, or it is just illegal use of variadic templates not reported here, and #1 is accepted incorrectly, or this is expected result? Piotr
Re: Variadic template function full specialization.
2008/11/19 Ben Elliston <[EMAIL PROTECTED]>: >> Following (bit weird ;-) code shows weird case of variadic template >> function specialization, however I am not sure it is legal, (atleast I >> haven't found any wording that would prevent such use): > > This list is not really the place to discuss language issues (unless you > think you've found a bug in g++). The [EMAIL PROTECTED] mailing list > is probably a better place to ask your question. > > Cheers, > Ben > > -- > Ben Elliston <[EMAIL PROTECTED]> > Australia Development Lab, IBM > > Hi, I was suppose to fill bug about, but I am not 100% sure what kind of bug it is: a) gcc should accept this code (I am in favour of this) b) gcc should reject specialization #1 that is why have asked here. I am sorry if it was not clear from my initial message. I am poor language lawyer, but I think that current behaviour is odd, and really couldn't find anything in n2798 paper, that would explain current behaviour. Thanks, Piotr
Re: optimization question: mpl
Hi, 2010/7/28 Hite, Christopher : >> Generally without knowing the compiler version you are using >> it is hard to tell. > I'll use whatever's best. Right now I'm still on 4.4.3. I'll probably > upgrade soon to 4.5. > >> The same is true without a complete compilable testcase. > I didn't want to make a test case that depends on boost::mpl. How's > this: > > struct DecodeContext; > > struct M1{ > static const int id=1; > static void decode(DecodeContext& ){} > }; > > struct M2{ > static const int id=2; > static void decode(DecodeContext& ){} > }; > > struct M3{ > static const int id=3; > static void decode(DecodeContext& ){} > }; > > > template struct ListMember; > > template <> struct ListMember<1>{ typedef M1 type; }; > template <> struct ListMember<2>{ typedef M2 type; }; > template <> struct ListMember<3>{ typedef M3 type; }; > > template > void foo(int id, DecodeContext& dc) { > typedef typename ListMember::type M; > if(M::id==id) > M::decode(dc); > else > foo(id,dc); > } > > template<> > void foo<0>(int id, DecodeContext& dc) {} > > > > int main(){ > DecodeContext& dc= *(DecodeContext*)0;// junk for now > int id=2; //sometime runtime dependent > foo<3>(id,dc); > return 0; > } > > >> You can use the flatten attribute to tell the compiler to inline all >> calls in a given function, like >> >> void __attribute__((flatten)) foo(void) >> { >> ... >> decode1(); >> ... >> } > > That would cause decode1() to be inlined, which might not be what you > want. > > Hmm maybe I could rewrite things so the switch case returns a function > pointer. I'm guessing that would make things slower though. > Or you could just initialize static array of pointers, like that (please note, that I never compiled code below): typedef void (*pfn) (DeclContext&); tempate struct InitDispatchHelper { static void init(std::tr1::array& a) { a[I-1] = ListMemeber::foo; InitPointersHelper::init(a); } }; // End recursion template<> struct InitDispatchHelper { static void init(std::tr1::array& a) {/*does nothing */ } }; // Dispatch table; template struct DispatchFoo : std::tr1::array { DispatchFoo() : std::tr1::array() { InitDispatchHelper::init(*this); } }; then your call would look like: static const int X = /* your maximal I in ListMembers meta-class */ void call_foo(int i, DeclContext& c) { static const DispatchFoo foo_dispatch; foo_dispatch[i] (c); } Bonus points for benchmarking both solutions and making it policy class choosing between those two solutions depending on X. Good luck, Piotr
Implementation of string-literal as template parameter
Hi, Recently i have posted an idea of new language feature to comp.lang.c++.moderated. That is "String literal as template parameter?" form 12.04.2008, if anyone is interested to see whole thread. The idea is to allow string-literals as template argument, and make them equivalent to variadic character pack 'char...'. For example: template class Foo; Foo<"abc"> would be equivalent to Foo<'a', 'b', 'c'> I am interested in writing proof of concept implementation. Since i dont have expirience with g++ codebase, I wanted to ask: 1) Is it expected to be hard to implement? (which I rather dont expect to be, but better ask :-) 2) Any hints, how/where should I start? Thanks in advance for any feedback. Piotr Rak