Re: [Boost-users] boost::result_of error?

Thanks for your clarifications, In your example, if "MyFunc" was typedef'd before its use in the template argument as say typedef int (*MyFunc)(int); it would not conflict with its use in the template argument ? best regards, ----- Original Message ----- From: "Ovanes Markarian" To: boost-users@lists.boost.org Subject: Re: [Boost-users] boost::result_of error? Date: Thu, 1 May 2008 17:13:40 +0200 Hicham, it does not create a new new type it only declares a type. Consider the following example: template<int ()> struct X {}; This means your type X expects a function pointer type with return value int and no parameters. This can be used in a meta programming to make some type assumptions or inspection. This is how the result_of works. Think of result_of as of type X but a bit more complex and which can give an assumption about the function type passed. We can write: template<int ()> struct X { typedef int type; }; A more generic form would be if int would be any valid result type T, so that we could write smth like this: template<T ()> struct X { typedef T type; }; Actually this is not a legal C++ struct, since T was not declared as template parameter, but think of it as if it would. template<T MyFunc()> struct X { typedef T result_type; typedef MyFunc function_type; }; Here MyFunc is a name of the function pointer type, which allows us to reference this type from inside the template. Hope that helps. With Kinds regards, Ovanes On Thu, May 1, 2008 at 2:32 PM, Hicham Mouline <hicham@mouline.org> wrote: But "floatfct" was defined earlier as the type of a pointer to a function with only 1 arg float.just passing the template argument to result_of creates a new type?is the new type also called floatfct ? ----- Original Message ----- From: "Ovanes Markarian" To: boost-users@lists.boost.org Subject: Re: [Boost-users] boost::result_of error? Date: Thu, 1 May 2008 13:58:21 +0200 Hicham, as far as I understand your code you pass to result_of a new type: typedef typename boost::result_of<floatfct(float, float)>::type resultype; This type is a pointer to a function type, which has as return type a pointer to floatfct and as params float, float. That's why it compiles. Regards, Ovanes On Thu, May 1, 2008 at 1:25 PM, Hicham Mouline <hicham@mouline.org> wrote: Hello, trying out Pete Becker's "c++ std lib ext" exercises, ex1 p155 #include <iostream> #include <typeinfo> #include <boost/utility/result_of.hpp> typedef float (*floatfct)(float); int main(int argc, char* argv[]) { typedef typename boost::result_of<floatfct(float, float)>::type resultype; std::cout<< typeid(resultype).name() << std::endl; } should fail, because result_of is instantiated with a callable type with 2 float args, while it's been defined as taking 1 float arg only? with intel10.1-MSVC8-boost1.35, it links. rds, _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Please read below. Just got an angry email from some pedantic Mailing-List guard. On Thu, May 1, 2008 at 6:07 PM, Hicham Mouline <hicham@mouline.org> wrote:
Thanks for your clarifications,
In your example, if "MyFunc" was typedef'd before its use in the template argument as say typedef int (*MyFunc)(int); it would not conflict with its use in the template argument ?
First of all. I am sorry for an error I made when stated: template<T MyFunc()> struct X { typedef T result_type; typedef MyFunc function_type; }; This is not a valid C++ code, since MyFunc as the template parameter is a real pointer to a function, it is not a type. And you cannot make a typedef for a pointer. Only for it's type. To visualize the usage I have written a small program: #include <iostream> #include <typeinfo> typedef int (*MyFunc)(int, int); int get_int(int x) { return x; } int plus(int x, int y) { return x+y; } template<int (*MyFunc)(int)> struct X { typedef int result_type; result_type exec()const { ::MyFunc f2 = + //use scope resolution to access the global MyFunc return MyFunc(10)+f2(10,10); } }; int main(int argc, char* argv[]) { X<&get_int> x; std::cout << "sum: " << x.exec() << "\n"; }
best regards,
participants (2)
-
Hicham Mouline
-
Ovanes Markarian