1

There is a class with three templates.

#if defined(USE_CACHE_FALRU)
template class Cache<FALRU>;
#endif

#if defined(USE_CACHE_IIC)
template class Cache<IIC>;
#endif

#if defined(USE_CACHE_LRU)
template class Cache<LRU>;
#endif

These template has common function:

FALRUBlk* accessBlock(Addr addr, int &lat, int context_src, int *inCache = 0);
IICTag* accessBlock(Addr addr, int &lat, int context_src);
BlkType* accessBlock(Addr addr, int &lat, int context_src);

As you can see, number of arguments for one of the templates differs from others.

Now in Cache(), there is a function which call accessBlock()

template<class TagStore>
bool Cache<TagStore>::access(PacketPtr pkt, BlkType *&blk, int &lat, PacketList &writebacks)
{ 
 ...
 blk = tags->accessBlock(pkt->getAddr(), lat, id);
 ...
}

In a confing file, all templates are defined

#define USE_CACHE_LRU 1
#define USE_CACHE_FALRU 1
#define USE_CACHE_IIC 1 

I wonder how this file is compiled. As you can see, FALRUBlk::accessBlock() takes 4 arguments. However in Cache::access(), only three arguments are passed. So Can someone explain how this function compiled without problem?

2 Answers 2

3

It compiles fine, because the default value for the 4th is used :

FALRUBlk* accessBlock(Addr addr, int &lat, int context_src, int *inCache = 0);

Other 2 have only 3 arguments :

IICTag* accessBlock(Addr addr, int &lat, int context_src);
BlkType* accessBlock(Addr addr, int &lat, int context_src);

Therefore, you can always call that method like in your example :

blk = tags->accessBlock(pkt->getAddr(), lat, id);
Sign up to request clarification or add additional context in comments.

4 Comments

So isn't "number of arguments must match" important?
@mahmood Off course the arguments must match (otherwise you'd get a compilation error). In the 1st case, since you call it by passing 3 values, 0 is automatically assigned to inCache variable).
The problem is when I add another argument to BlkType* accessBlock (now takes four arguments), compiler says error that can not convert my fourth argument to int* (which is fourth argument of FALRUBlk*)
@mahmood See for example this page on default arguments
0

In C++, and most other programming languages, you're allowed to set a default value for arguments to a function. In C++ specifically, you can even provide default template arguments for your templatized classes.

The general rule is that the default arguments go at the end. You must put parameters with default arguments last in your arguments list. You can have any number of parameters with default arguments - for all you care, your function could have 10 parameters each with a default value.

If that were the case, your 10-argument function could be called without passing any aguments in - because for all intensive purposes, the function would act as if you called it with the values specified as the defaults.

You use this more than you might think. For a slightly more complex example, STL associative containers (like std::set) have ordering. They provide a "default template argument" of std::less<> saying that "items in this container must be sorted using their < operator". They also provide another default template argument for the allocator which defines their memory management interface.

Those are completely hidden from you unless you decide to change them by "overriding" the default arguments., which is why you're able to create a set with just std::set<DataType> when the real type would be something looking more like std::set<Key, Compare, Allocator<Key> >;.

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.