This is a question of what do do for the elementsSize() member function, regarding the automatic return type deduction:
#include <iostream>
#include <vector>
template<typename Element>
class ElementVector
{
std::vector<Element> elementVec_;
// Other attributes.
public:
ElementVector() = default;
ElementVector(const std::initializer_list<Element>& list)
:
elementVec_(list)
{}
auto elementsSize() // -> decltype(elementVec_size())
{
return elementVec_.size();
}
};
using namespace std;
int main(int argc, const char *argv[])
{
ElementVector<double> e = {1.2, 1.3, 1.4, 1.5};
cout << e.elementsSize() << endl;
return 0;
}
The code above results in a compiler warning (gcc 4.8.2):
main.cpp:20:27: warning: ‘elementsSize’ function uses ‘auto’ type specifier without trailing return type [enabled by default]
auto elementsSize() // -> decltype(elementVec_size())
I have read about the option of automatic return type deduction being made possible for C++14 without the use of decltype.
Writing the commented out decltype
seems weird to me somehow. What am I doing wrong?
Note: I know that I could inherit from std::vector if there is no vector among "Other attributes", which is precisely the case in my actual problem.
What am I doing wrong?
Nothing. GCC 4.8 implements auto-deduced return types, but as an enabled-by-default C++1y feature. Compiling with -std=c++1y
will remove that warning.
[Answer converted from this comment.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With