c++ - How can I have the compiler use the smallest type in a template 'const' argument without C++11 for use with a (constrained) 8-bit micro-controller? -


i have template class use avr library. (quite basic) scheduler. constraint have as possible determined during compile-time. here's 1 (stripped-down) example:

template <const unsigned n> void foo::bar() {     static unsigned k = n;     ... } 

the thing don't have use 2-byte unsigned integer when use smallest type fits. trouble can't (yet) use c++11 standard because avr compiler (the version of avr-gcc i'm using 5.3.0) hasn't enabled part of standard. guess write same template function thrice type ranging uint8_t uint32_t don't either.

so question is: [how] can manage have compiler deduce shortest type const argument of template function repeat ?

edited: initial title of question relied upon misunderstanding of auto keyword. want know still remains: have compiler select shortest/fittest type const template argument. why haven't used more appropriate title in first place? have no answer that, unfortunately.

the thing don't have use 2-byte unsigned integer when use smallest type fits, (if got right) auto does

this not @ auto does. auto deduces type declaration based on type of initialization expression. example, auto = 1 declares int, , auto b = 1l declares long, despite value 1 fitting within short, or char fine. deduction has nothing being smallest.

to answer question (in case else needs it), there boost_auto macro, "emulates proposed auto keyword in c++" in words of documentation. so, that's asked for, suspect it's not want.

but, there thing in boost seem want: <boost/integer.hpp> header has collection of templates allow integer type selection based on maximum required representable value.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -