Same Typely Funcs Definition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <typeinfo>
using namespace std;
int add(){
    int a,b;
    a=25;
    b=35;
    return (a+b);
    }
   
   (typeid(add))transaction(){  }
    
    
    int main(){
      cout<<transaction()<<endl;
        system("pause");
        return 0;
        }


How can i definition "transaction()" func.
How about
1
2
3
4
5
6
7
8
9
10
template <typename T>
struct foo{
    static T add(){ /*...*/ }
    static T transaction(){ /*...*/ }
};

//...

foo<int>::add();
foo<int>::transaction();
?
Last edited on
or
1
2
3
4
auto transaction() -> decltype(add())
{
/* */
}


But really, go with what helios said.
Yes thank u.hanst99's answer is my answer.
Not really. helios version is better suited in most cases, my version is mostly for template functions where the result can't be easily determined, like this:

1
2
3
4
5
template<typename T1, typename T2>
auto add(T1 left, T2 right) -> decltype(left+right)
{
return left+right;
}


here left+right may either be T1, T2 or something completely different, that's why you need the decltype specification. But with the example you showed us, helios version is better.
Topic archived. No new replies allowed.