1

I want to create a function that returns different types of data-types for different input string. I am using templates for it but seems like I am making some mistake.

template<typename S>
S select(string type){

int integer;
float floaty;
char character;
string strings;

if(type=="int")
    return integer;

if(type=="char")
    return character;

if(type=="float")
    return floaty;

if(type=="string")
    return strings;
}

it gives this error when I run it will string argument int .

    sam.cpp:771:13: error: no matching function for call to ‘select(std::string&)’
  select(type);
             ^
sam.cpp:771:13: note: candidates are:
In file included from /usr/include/x86_64-linux-gnu/sys/types.h:219:0,
                 from /usr/include/stdlib.h:314,
                 from Markup.h:12,
                 from sam.cpp:3:
/usr/include/x86_64-linux-gnu/sys/select.h:106:12: note: int select(int, fd_set*, fd_set*, fd_set*, timeval*)
 extern int select (int __nfds, fd_set *__restrict __readfds,
            ^
/usr/include/x86_64-linux-gnu/sys/select.h:106:12: note:   candidate expects 5 arguments, 1 provided
sam.cpp:17:3: note: template<class S> S select(std::string)
 S select(string type){
   ^
sam.cpp:17:3: note:   template argument deduction/substitution failed:
sam.cpp:771:13: note:   couldn't deduce template parameter ‘S’
  select(type);

If it is wrong way and there is a better way of doing things then do share, Thanks.

2

2 Answers 2

3

In C++ template type deduction is based on parameter and not on return type so, in your particular case, when you are calling the function select, you have to explicitly specify the template argument.

then how will I achieve what I want to do with this function?

Use template specialization.

template<typename S>
S select(){
static_assert("Not Implemented");
}

template<> int select<int>() {
    int integer;
    //To Do
    return integer;
}

template<> float select<float >() {
    float floaty;
    //To Do
    return floaty;
}
//Remaining Specialization

and call the respective specialization using explicit template parameter

int main()
{
    int _integer = select<int>();
    float _float = select<float>();
    ..........
}
Sign up to request clarification or add additional context in comments.

8 Comments

then how will I achieve what I want to do with this function?
@Bramsh You won't. C++ is statically typed, while you want the return type of select to depend on a runtime value (the content of type). Look into something like Boost.Variant or Boost.Any.
@Angew: Though the question was not well framed, but what he wants to achieve can easily be done through template specialization.
@Abhijit How? The solution you've presented doesn't use the OP's type parameter at all.
@Bramsh: The important thing to note is, if you know and can decide on the type during compile time using static polymorphism (templates), if the type is not known until runtime, use dynamic polymorphism (virtual functions, RTTI (dynamic cast))
|
0

There's no way this can work. Templates require their parameters to be known at compile time, but the value of type is only known at run time.

If S is int then return strings won't compile, but if S is string then return integer won't compile.

And as others have pointed out S cannot be deduced, so you have to specify it explicitly in the call. But it still can't work for the reason above.

(Quite apart from all that, you haven't initialised any of the values.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.