[function_types] Number of default parameters

Hello all, Is there a way to determine at compile-time the number of parameters with default values from a function type? For example, can I program the following `count_defaults` metafunction? count_defaults< bool(*)(int x, int y) >::value // == 0 count_defaults< bool(*)(int x, int y = 1 >::value // == 1 count_defaults< bool(*)(int x = 2, int y =1) >::value // == 2 (If this is possible, it could be added to Boost.FunctionTypes.) Thank you. -- Lorenzo

On 2/8/2011 5:38 PM, Lorenzo Caminiti wrote:
Hello all,
Is there a way to determine at compile-time the number of parameters with default values from a function type?
The following test proggy suggestions "no" :( Default parameters aren't part of the signature of a function. #include <iostream> #include <boost/mpl/assert.hpp> void f(int) { } void g(int = 0) { } typedef int yes_t; typedef char no_t; template< class T > yes_t is_same(T,T); template< class T, class U > no_t is_same(T,U); int main() { BOOST_MPL_ASSERT_RELATION( sizeof( is_same(&f,&g) ), ==, sizeof( yes_t ) ); return 0; } - Jeff

On Tue, Feb 8, 2011 at 8:38 PM, Lorenzo Caminiti <lorcaminiti@gmail.com> wrote:
Is there a way to determine at compile-time the number of parameters with default values from a function type?
Nope, it's not part of the type. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

On Tue, Feb 8, 2011 at 8:50 PM, Dave Abrahams <dave@boostpro.com> wrote:
On Tue, Feb 8, 2011 at 8:38 PM, Lorenzo Caminiti <lorcaminiti@gmail.com> wrote:
Is there a way to determine at compile-time the number of parameters with default values from a function type?
Nope, it's not part of the type.
OK, not possible. Thanks all for the quick replies. I'm confused because the following code doesn't compile on MSVC but it _does_ compiles fine on GCC... #include <boost/function_types/parameter_types.hpp> #include <boost/mpl/at.hpp> int main() { typedef void (func_type)(double num = -1.23); // NO STANDARD?? typedef boost::function_types::parameter_types<func_type>::type param_types; typedef boost::mpl::at_c<param_types, 0>::type arg_type0; return 0; } 1) Is this a bug in GCC which should not accept `= -1.23` in `func_type`? 2) Both, GCC and MSVC accept the parameter name `num` as part of the `func_type` type. Can parameter names be specified in function types? What does the C++ standard say? Thanks a lot! -- Lorenzo

[Lorenzo Caminiti]
I'm confused because the following code doesn't compile on MSVC but it _does_ compiles fine on GCC... typedef void (func_type)(double num = -1.23); // NO STANDARD?? 1) Is this a bug in GCC which should not accept `= -1.23` in `func_type`?
VC is correct to reject this code. (So does EDG.) C++03 8.3.6 "Default arguments" [dcl.fct.default]/3: "A default argument expression shall be specified only in the parameter-declaration-clause of a function declaration or in a template-parameter (14.1). If it is specified in a parameter-declaration-clause, it shall not occur within a declarator or abstract-declarator of a parameter-declaration.88)" Footnote 88: "This means that default arguments cannot appear, for example, in declarations of pointers to functions, references to functions, or typedef declarations."
2) Both, GCC and MSVC accept the parameter name `num` as part of the `func_type` type. Can parameter names be specified in function types?
Yes, that's totally fine, they're just ignored. C++03 8.3.5 "Functions" [dcl.fct]/8: "An identifier can optionally be provided as a parameter name", which applies to typedefs too. Stephan T. Lavavej Visual C++ Libraries Developer

On Tue, Feb 8, 2011 at 9:28 PM, Stephan T. Lavavej <stl@exchange.microsoft.com> wrote:
[Lorenzo Caminiti]
I'm confused because the following code doesn't compile on MSVC but it _does_ compiles fine on GCC... typedef void (func_type)(double num = -1.23); // NO STANDARD?? 1) Is this a bug in GCC which should not accept `= -1.23` in `func_type`?
VC is correct to reject this code. (So does EDG.)
C++03 8.3.6 "Default arguments" [dcl.fct.default]/3: "A default argument expression shall be specified only in the parameter-declaration-clause of a function declaration or in a template-parameter (14.1). If it is specified in a parameter-declaration-clause, it shall not occur within a declarator or abstract-declarator of a parameter-declaration.88)"
Footnote 88: "This means that default arguments cannot appear, for example, in declarations of pointers to functions, references to functions, or typedef declarations."
2) Both, GCC and MSVC accept the parameter name `num` as part of the `func_type` type. Can parameter names be specified in function types?
Yes, that's totally fine, they're just ignored. C++03 8.3.5 "Functions" [dcl.fct]/8: "An identifier can optionally be provided as a parameter name", which applies to typedefs too.
Wow! Thanks a lot for all the standard references -- that's very accurate information! Also, are the function parameter storage classifiers `auto` and `register` part of the function type? Can I manipulate (check, add, and remove) them at compile-time using template metafunctions? The expression `typedef int (f_type)(auto int x, register bool y);` complies on both GCC and MSVC so maybe they are part of the function type... NOTE: This is more of a curiosity because for my application I can manipulate the storage classifiers using preprocessor metaprogramming. In fact, `auto` and `register` are known alphanumeric tokens (i.e., keywords) that always appear at the beginning of the parameter type expression so the following macros can be used to manipulate them: #include <boost/preprocessor.hpp> #include <boost/preprocessor/detail/is_unary.hpp> #define IS_AUTO_auto (1) /* must expand to unary */ #define IS_AUTO(tokens) BOOST_PP_IS_UNARY(BOOST_PP_CAT(IS_AUTO_, tokens)) #define REMOVE_AUTO_STRIP_auto /* must expand to nothing */ #define REMOVE_AUTO_(tokens) BOOST_PP_CAT(REMOVE_AUTO_STRIP_, tokens) #define REMOVE_AUTO(tokens) \ BOOST_PP_IIF(IS_AUTO(tokens), \ REMOVE_AUTO_ \ , \ tokens BOOST_PP_TUPLE_EAT(1) \ )(tokens) #define ADD_AUTO(tokens) \ BOOST_PP_EXPR_IIF(BOOST_PP_NOT(IS_AUTO(tokens)), auto) tokens IS_AUTO(auto int x) // 1 IS_AUTO(int x) // 0 REMOVE_AUTO(auto int x) // int x REMOVE_AUTO(int x) // int x ADD_AUTO(auto int x) // auto int x ADD_AUTO(int x) // auto int x (These macros work as long as the type expression starts with an alphanumeric token -- if not, I can wrap the type expression using Boost.Local's `BOOST_IDENTITY_TYPE` as in `BOOST_IDENITY_TYPE(::std::string) s`.) -- Lorenzo

[Lorenzo Caminiti]
Also, are the function parameter storage classifiers `auto` and `register` part of the function type? Can I manipulate (check, add, and remove) them at compile-time using template metafunctions?
The expression `typedef int (f_type)(auto int x, register bool y);` complies on both GCC and MSVC so maybe they are part of the function type...
auto's dead. As for register, I'm not a Core Language guru and I can't find Standardese saying anything one way or the other (I see the Standardese that drops top-level cv-qualifiers), but both VC10 RTM and GCC 4.5.2 appear to believe that register does *not* affect the function type: C:\Temp>type meow.cpp #include <ios> #include <iostream> #include <ostream> #include <typeinfo> #include <type_traits> using namespace std; int main() { cout << boolalpha; cout << typeid(void (int, register int)).name() << endl; cout << is_same<void (int, register int), void (int, int)>::value << endl; } C:\Temp>cl /EHsc /nologo /W4 meow.cpp && meow meow.cpp void __cdecl(int,int) true C:\Temp>g++ -Wall -Wextra -std=c++0x meow.cpp -o meow.exe && meow FviiE true C:\Temp>c++filt -t FviiE void (int, int) STL

AMDG On 2/8/2011 5:38 PM, Lorenzo Caminiti wrote:
Is there a way to determine at compile-time the number of parameters with default values from a function type?
For example, can I program the following `count_defaults` metafunction?
count_defaults< bool(*)(int x, int y)>::value // == 0 count_defaults< bool(*)(int x, int y = 1>::value // == 1 count_defaults< bool(*)(int x = 2, int y =1)>::value // == 2
(If this is possible, it could be added to Boost.FunctionTypes.)
No. The default parameters are not part of the type. In Christ, Steven Watanabe
participants (5)
-
Dave Abrahams
-
Jeffrey Lee Hellrung, Jr.
-
Lorenzo Caminiti
-
Stephan T. Lavavej
-
Steven Watanabe