-1

I am currently seen some c++11 code in the project , where i got little confused with some funky syntax.

below is the code

std::shared_ptr<CommonAPI::Runtime> runtime_AMB = CommonAPI::Runtime::get();
std::shared_ptr<v1::org::table::psa::EthernetProxy<>> amb_consumer ;
amb_consumer = runtime_AMB->buildProxy<v1::org::table::psa::EthernetProxy>();

Here my doubt is "buildProxy" function, it can be simply called why its mention like

buildProxy<v1::org::table::psa::EthernetProxy>() instead of buildProxy()

One more doubt is shared_ptr<v1::org::table::psa::EthernetProxy<>> here why EthernetProxy<> instead just like v1::org::table::psa::EthernetProxy

May be its easy but i am not aware of c++11 thatmuch

1 Answers1

2

Answer to the first question

Say you have:

struct Foo
{
    template <typeename T>
    T bar() { return T{}; }

    template <typeename T>
    T baz(T t) { return 2*t; }
};

To use Foo::bar, you need to provide a template parameter.

Foo foo;
foo.bar<int>(); // OK.
foo.bar();      // Not OK.

If the template parameter can be deduced from the arguments, then you don't need to explicitly specity the template parameter.

foo.baz<int>(10); // OK. Template parameter is explicity.
foo.bar(10);      // Also OK. Template parameter is deduced to be int

Answer to the second question

EthernetProxy seems to be a class template with a default template parameter. Say you have:

template <typename T = int> struct EthernetProxy { ... };

EthernetProxy is not a class, it is a class template. An instatiation of the class template will be a class.

EthernetProxy<double> var1; // OK
EthernetProxy<> var2;       // Also OK. The default template parameter int is used.

That's why you can use EthernetProxy<> as a type but not EthernetProxy.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thanks @R sahu, atleast you answered it instead of just making -ve, really helpfull , i have one doubt is like "return T{};" It means when our return type is template type then we have to mentioned explicitly ? – Tuhin Panda Sep 22 '19 at 05:33
  • @TuhinPanda, take a look at https://en.cppreference.com/w/cpp/language/value_initialization. I hope you are learning the language from a good book. If not, I strongly advise you to do so. You can start by looking at [a list of good textbooks](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Sep 22 '19 at 05:38