0

In STL, the syntax for Fill is template void fill (ForwardIterator first, ForwardIterator last, const T& val);

The third parameter in the below program should be int. What does it imply by the statement operator int() const? Can we have int as a function name? How does the return value of this function get converted to int and stored in a vector(As in third parameter of fill function)?

#include <vector> #include <iostream> #include <algorithm> using namespace std; void print(int v) { cout << v << ", "; } struct Sequence { int start; Sequence(int a,int b): start(a) { } Sequence(int start) :start(start) {} operator int() const{ return start;//LINE I } }; int main() { vector<int> v1(7); fill(v1.begin(), v1.end(), Sequence(1,2));//LINE II for_each(v1.begin(), v1.end(), print); return 0; } 
2
  • @WhiZTiM -- it's called a conversion operator. A cast is not the only thing that triggers a conversion. Commented Aug 14, 2016 at 21:47
  • @PeteBecker, Thanks for the correction. Noted! Commented Aug 14, 2016 at 22:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.