see these sample:
template <typename a>
class test
{
test(a argument)
{
cout << argument;
}
};
//declare it:
int main()
{
test<string> a("hello");
}
how can i do it:
test a("hello");
see these sample:
template <typename a>
class test
{
test(a argument)
{
cout << argument;
}
};
//declare it:
int main()
{
test<string> a("hello");
}
how can i do it:
test a("hello");
The closest you can get without using the preprocessor is providing a default type:
template <typename a = string>
class test
...
test<> a("hello");
why here don't use the template arguments?
see page 3('codigo-fonte'): https://dl.dropboxusercontent.com/u/28818654/Community/templates.pdf
You didn't point out exactly where you're talking about in that document, but I'm guessing you mean the template functions. If a parameter in a template function uses one of the template type parameters, calls to that function will deduce the type of the template parameter based on the type of the argument:
template <typename T>
void foo(T arg)
{
cout << arg << '\n';
}
int main()
{
foo(123); // T deduced as int
foo(123.456); // T deduced as double
}
This doesn't work for template classes because there's nothing to deduce. Of course, if you're okay with using a strict template type parameter, you could use typedef
or a C++11 alias:
typedef test<string> test_t;
test_t a("hello");
What deceptikon said. In addition, I want to mention that this deduction of the template arguments is also the reason for having the make_foo
functions to create class templates in a way that avoids explicitely listing the template arguments. What you would do is this:
template <typename A>
class test
{
test(A argument)
{
cout << argument;
}
};
template <typename A>
test<A> make_test(A argument) {
return test<A>(argument);
};
//declare it:
int main()
{
make_test("hello");
// or, in C++11, with the 'auto' type deduction:
auto a = make_test("hello");
return 0;
};
This is a classic technique, used in many places, for example, in the STL, with class templates like std::pair
, std::tuple
, std::shared_ptr
, etc...
thanks, these classic technique have a term name?
(for google search: i want read more about it)
i belive theres another way.
see my code:
#ifndef events_H_INCLUDED
#define events_H_INCLUDED
#include <functional>
#include <vector>
template <typename ... b>
class event
{
public:
typedef std::function<void(b...argx )> OnSomethingHandler;
event(OnSomethingHandler Handler)
{
handlers_=Handler;
}
void operator ()(b... args)
{
handlers_(args...);
}
event& operator = (OnSomethingHandler Handler)
{
handlers_ = Handler;
return *this;
}
private:
OnSomethingHandler handlers_;
};
#endif // events_H_INCLUDED
how i use it:
event<int,int> Soma{[](int a, int b) { ; }};
someone tell me that i can change the my template for not repeat the same type, can you advice me?
#define events(eventname, ... ) event<__VA_ARGS__> eventname{[](__VA_ARGS__ ) { ; }};
what isn't right with these macro?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.