How to invoke functions automaticly when it calls this function.
-
giovedì 12 aprile 2012 08:22
code:
#define call (str) iwillcall(##strName##str##)
fuction1(str1){ call( str1); }
fuction2(str2){ call(str2);}
template<typename T> iwillcall(str){ cout << t.name<< str;}
void main(void){ function1("i am called"); }
result:
function1i am called
I want to make it work, how?
Tutte le risposte
-
giovedì 12 aprile 2012 10:51
I don't really know what you want to do, but I suspect this:
#include <iostream> #include <string> void iwillcall(std::string func, std::string str) { std::cout << func << str; } #define call(str) iwillcall(__FUNCTION__, str) void function1(std::string str1) { call(str1); } void main() { function1("i am called"); }
If not, please state your actual requirements...
-
venerdì 13 aprile 2012 01:56Well, i think it's part. but this type of work is not safe,right?Would you please give me a safer one. Which at least can know when the function1 is not a kind of "typename T", So template is in the requirements.
-
venerdì 13 aprile 2012 06:34
I do not really get you, but let me make an educated guess. I suppose that the function "iwillcall" must not only take a std::string, but an arbitrary type...
#include <iostream> #include <string> template<typename T> void iwillcall(std::string func, T t) { std::cout << func << t; } #define call(t) iwillcall(__FUNCTION__, t) void function1(std::string str1) { call(str1); } void main() { function1("i am called"); }If you want an arbitrary number of parameters, you should use the C++11 variadic templates not yet available in VC++: http://en.wikipedia.org/wiki/Variadic_templates.
I hope I get you right :)
-
domenica 15 aprile 2012 03:22I want the func to be an arbitrary one, As the modern c++ design call it "Compile Time Assertions", the "call(t)" function can check if the parameter satisfies the check that it brings only one paremeter with the type of string.
-
domenica 15 aprile 2012 03:32
Genrge wrote:
I want the func to be an arbitrary one, As the modern c++ design call it "Compile Time Assertions", the "call(t)" function can
check if the parameter satisfies the check that it brings only one paremeter with the type of string.If you want a function that takes one parameter of type string, why don't you just declare it that way? Why make it a template?
Igor Tandetnik
-
domenica 15 aprile 2012 09:06
Well, what i need is a check, not a way to make it work.
-
domenica 15 aprile 2012 09:09I need to check if you have the right function invoked. if the function statisfies the "typename T" with a parameter of type string. then the function is allowed to be invoked.
-
domenica 15 aprile 2012 13:07
Genrge wrote:
Well, what i need is a check, not a way to make it work.
You don't need a way to make your program work? I'm confused.
What problem are you really trying to solve? Describe the original problem, not your proposed solution (which doesn't make much sense, no offense intended).
Igor Tandetnik
-
lunedì 16 aprile 2012 01:57
If you might give me the code, that would be better. Here
I need to check if you have the right function invoked. if the function statisfies the "typename T" with a parameter of type string. then the function is allowed to be invoked.
is what i really need.
-
lunedì 16 aprile 2012 02:46No, that's not answering Igor's question. What is the nature of your application? What is it supposed to do?
-
lunedì 16 aprile 2012 03:10
Ok, let's make it clear like this.
#include <iostream>
#include <string>
template<typename T> void iwillcall(std::string func, T t)
{
std::cout << func << t;
}
#define call(t) iwillcall(__FUNCTION__, t)
void function1(std::string str1)
{
call(str1);
}void function2(int it2){ call(it2); }
void main()
{
function1("i am called");
}The function1(string str1) is good enough to solve the problem that i want to output the function name and the string "i am called".But what if i invoke the function2("i am called"), or the function2(1)? I want the complier to know the truth that these kind of use of the function is not allowed and output the result in a better way.
-
lunedì 16 aprile 2012 04:24
Genrge wrote:
void function2(int it2){ call(it2); }
But what if i invoke the function2("i am called")You'll get a compiler error - can't convert char[12] to int.
or the function2(1)?
That would work. Don't you want it to?
I want the complier to know the truth that these
kind of use of the function is not allowedWhy is function2(1) not allowed? Can you show an example of an allowed way to call function2?
and output the result in a better way.
Better than what other way? Better in what sense?
Igor Tandetnik
-
lunedì 16 aprile 2012 05:07
Yeap, i don't want the function2(1) work;
-
lunedì 16 aprile 2012 05:08So, here is the problem, how to make the function2(1) not work.
-
lunedì 16 aprile 2012 05:10
When you invoke funtion2(1), i want the compliler output an exception.
-
lunedì 16 aprile 2012 07:00That's strange thinking... If you don't want function2 to be called with an int, just don't define it. define void function2(std::string) instead for example.
-
lunedì 16 aprile 2012 07:52Here we go trying to nail jelly to the wall again ...
>void function2(int it2)
>i don't want the function2(1) work;
What int values *do* you want it to accept?
Since you have defined function2 as taking an
int for an argument, and 1 is an int, on what
basis should function2(1) be rejected?
>void function2(int it2){ call(it2);
Or are you saying that function2 should not be
allowed to invoke the "call()/iwillcall()"
function?
- Wayne
-
lunedì 16 aprile 2012 08:23
So, here is the problem, how to make the function2(1) not work.
#include <iostream>
#include <string>
//template<typename T> void iwillcall(std::string func, T t)
void iwillcall(std::string func, std::string t)
{
std::cout << func << t;
}
#define call(t) iwillcall(__FUNCTION__, t)
void function1(std::string str1)
{
call(str1);
}
void function2(int it2){ call(it2); }
int main()
{
function1("i am called"); // OK
function2(1); // error
}
- Wayne
-
lunedì 16 aprile 2012 10:46
Or are you saying that function2 should not be
allowed to invoke the "call()/iwillcall()"
function?
Yes , function2 should not be allowed to invoke the "call()/iwillcall()"
-
lunedì 16 aprile 2012 10:50
So, here is the problem, how to make the function2(1) not work.
#include <iostream>
#include <string>
//template<typename T> void iwillcall(std::string func, T t)
void iwillcall(std::string func, std::string t)
{
std::cout << func << t;
}
#define call(t) iwillcall(__FUNCTION__, t)
void function1(std::string str1)
{
call(str1);
}
void function2(int it2){ call(it2); }
int main()
{
function1("i am called"); // OK
function2(1); // error
}
- Wayne
Exactly the truth when function2 just take the parameter with type of string, what i want is the function2 take some kinds of types and these types might statisfies the "typename T" which iwillcall will use.
-
lunedì 16 aprile 2012 10:50Just don't call it... I do not really get where you are going with this. Maybe a context would help!
-
lunedì 16 aprile 2012 11:25
#include <iostream>
#include <string>class NullType;
template <class T, class U>
struct Typelist
{
typedef T Head;
typedef U Tail;
};#define TYPELIST_1(T1) Typelist<T1, NullType >
#define TYPELIST_2(T1, T2) Typelist<T1, TYPELIST_1(T2) >
//template<typename T> void iwillcall(std::string func, T t)template<typename T>
void iwillcall(std::string func, T t)
{
std::cout << func << t;
}
#define call(t) iwillcall(__FUNCTION__, t)
template<typename T>
void function1(T str1)
{
call(str1);
}int main()
{
//function1("i am called"); // OK
//function2(1); // error
typedef TYPELIST_3(std::string, int, double) tp;
function1<tp>("i am called");
function1<int>(2);
}please let the function1<tp>("i am called").
-
lunedì 16 aprile 2012 13:16
Genrge wrote:
template<typename T>
void function1(T str1)
{
call(str1);
}
int main()
{
typedef TYPELIST_3(std::string, int, double) tp;
function1<tp>("i am called");That won't compile. function1<tp> takes a parameter of type tp, not a string, and there is no conversion from string to tp. It doesn't matter what happens inside the body of the function - the compiler won't get that far.
please let the function1<tp>("i am called").
If you want to call it with a string as a parameter, you have to declare it with string as a parameter.
Igor Tandetnik
-
lunedì 16 aprile 2012 17:37
what i want is the function2 take some kinds of types and these types might statisfies the "typename T" which iwillcall will use.
If you want a function to work with some argument types
but not with others then overload it with the types you
want to be valid only.
e.g. -
void function1(std::string str1)
{
call(str1);
}
void function1(int intin)
{
call(intin);
}
void function1(double d)
{
call(d);
}
Be mindful of automatic promotion rules which may
allow non-specified types to match one of the
signatures. e.g. - A char can be promoted to an int.
- Wayne -
lunedì 16 aprile 2012 21:07
Another way is Specialized template . You can find plenty of article on this.
Thanks
Rupesh Shukla
-
martedì 17 aprile 2012 01:29
Another way is Specialized template . You can find plenty of article on this.
Thanks
Rupesh Shukla
Hi, would you give me a way to solve such problems. Thanks a lot. -
martedì 17 aprile 2012 03:02
Another way is Specialized template . You can find plenty of article on this.
Thanks
Rupesh Shukla
Hi, would you give me a way to solve such problems. Thanks a lot.A Simple example
#include <iostream> using std::cout; template <typename T> class Normal { public: Normal() { cout<<"Normal"; } }; template<> class Normal <int> //specialization { public: Normal() { cout<<" With Int"; } }; int main() { Normal <int> r; //will print with int Normal <float> ft; //will print Normal return 0; }
ThanksRupesh Shukla
-
martedì 17 aprile 2012 03:36
Hi,Rupesh, I want the paremeter TYPELIST_2(int, string) work.
Your way is the same as the way which declares a function and invoke.
-
martedì 17 aprile 2012 06:53As from http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/3d985fb1-519f-4270-9b20-a6f3f3bd2e8d, We get an answer that the compiler knows the value passed is from "a better match" way, and how might we offer the compiler "a better match" way to choose a better function?
-
martedì 17 aprile 2012 15:26
Hi,Rupesh, I want the paremeter TYPELIST_2(int, string) work.
Your way is the same as the way which declares a function and invoke.
Genrge it's not a same the compiler will make a call to the appropriate class on the basic of decl. You can see my both class name is Normal but parameter is different .
Thanks
Rupesh Shukla

