I have two different approaches to a templated function using auto and decltype but one of them does not compile, yielding two error:
Where is the main difference between the following versions of the code:
This compiles
template<typename MemoryMapper, typename EngineParts>
class EngineBase {
const MemoryMapper memoryMapper;
public:
const EngineParts engineParts;
EngineBase();
virtual ~EngineBase();
template<typename Mapper, typename MemoryAccessor>
void getMemoryManager(void);
template<MemoryMapper,typename MemoryAccessor>
auto getMemoryManager(void)->decltype(memoryMapper.getMemoryManager<MemoryAccessor>())
{
return memoryMapper.getMemoryManager<MemoryAccessor>();
}
};
While this does not
template<typename MemoryMapper, typename EngineParts>
class EngineBase {
const MemoryMapper memoryMapper;
public:
const EngineParts engineParts;
EngineBase();
virtual ~EngineBase();
template<typename MemoryAccessor>
auto getMemoryManager(void)->decltype(memoryMapper.getMemoryManager<MemoryAccessor>())
{
return memoryMapper.getMemoryManager<MemoryAccessor>();
}
};
The error output is as follows:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -o EngineBase.o "..\\EngineBase.cpp"
In file included from ..\EngineBase.cpp:7:0:
..\EngineBase.h:22:84: error: expected primary-expression before '>' token
auto getMemoryManager(void)->decltype(memoryMapper.getMemoryManager<MemoryAccessor>())
^
..\EngineBase.h:22:86: error: expected primary-expression before ')' token
auto getMemoryManager(void)->decltype(memoryMapper.getMemoryManager<MemoryAccessor>())
^
14:50:52 Build Finished (took 2s.587ms)
Since I don't want to specifically write the first template argument on every use and it would create the impression that there are more functions defined than realistically are, how can I get the second version to work without introducing another template argument? Function parameters would be ok, though.
template<typename MemoryAccessor>
auto getMemoryManager(void)->decltype(memoryMapper.template getMemoryManager<MemoryAccessor>())
// ~~~~~~~^
{
return memoryMapper.template getMemoryManager<MemoryAccessor>();
// ~~~~~~~^
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.