开发者

strange template problem

开发者 https://www.devze.com 2023-02-05 13:34 出处:网络
I have the following function defined: template<class KEY, class VALUE, class HASH_FUNCTION, class COMPARATOR_FUNCTION, class GREATER_THAN_FUNCTION>

I have the following function defined:

template<class KEY, class VALUE, class HASH_FUNCTION, 
         class COMPARATOR_FUNCTION, class GREATER_THAN_FUNCTION>
bool Test3(size_t szCount, double dLoadFactor, vector&开发者_运维百科lt;KEY>& vVals, 
           const HASH_FUNCTION& hf, const COMPARATOR_FUNCTION& cf, 
           const GREATER_THAN_FUNCTION& gf)

Then I call it in main().

int main()
{
    vector<char*> vVals = GetWords("enwik8", 128*1024*1024);
    SHash sh;
    SHComp cf;
    SHGreater gf;
    Test3(1000, 0.7f, vVals, sh, cf, gf);

    return 0;
}

And I get this error thrown at me:

/home/duminda/main.cpp:313: error: no matching function for call to 
    ‘Test3(int, float, std::vector<char*, std::allocator<char*> >&, SHash&, 
    SHComp&, SHGreater&)’

I maybe missing something obvious. Any help would be appreciated. Thanks.


Looking at the function declaration, it has a template parameter V which isn't used anywhere in the actual parameter list. So it can't be deduced automatically at the call site.

Try just defining it as this instead:

template<class K class HF, class CF, class GF>
bool Test3(size_t szCount, double dLoadFactor, vector<K>& vVals, const HF& hf, const CF& cf, const GF& gf)

By the way, you might want to consider spending 5 seconds coming up with meaningful names. If V had had a sensible name, it might have been more obvious that it serves no purpose.


There is a template parameter VALUE that has not been resolved. It is never used.

Your function should already be able to derive this type if it is using it somewhere in the body. If it derives from a different template parameter, ensure you have a way of doing so. Possibly that template parameter has an internal typedef, for example if one of your comparison functions compares values of this type, they could have a value_type typedef.

Be certain to use typename in the body to get to this type if it is done that way.


Given the edited declaration

template<class KEY, class VALUE, class HASH_FUNCTION,
         class COMPARATOR_FUNCTION, class GREATER_THAN_FUNCTION>
bool Test3(size_t szCount, double dLoadFactor, vector<KEY>& vVals,
           const HASH_FUNCTION& hf, const COMPARATOR_FUNCTION& cf,
           const GREATER_THAN_FUNCTION& gf);

and the comment: V is used by the function Test3. Is there a way for me to tell the compiler what the type of V is?

The problem with Test3(1000, 0.7f, vVals, sh, cf, gf); is that the compiler cannot deduce the template argument VALUE. But you can explicitly list template arguments to a function name like this:

Test3<const char*, value_type>(1000, 0.7f, vVals, sh, cf, gf);

Here the first two template arguments KEY=const char* and VALUE=value_type are given in < angle brackets >, and the rest can be deduced by the compiler.

If you change the order of KEY and VALUE in the template declaration, you could skip providing the KEY and let that be deduced:

template<class VALUE, class KEY, class HASH_FUNCTION,
         class COMPARATOR_FUNCTION, class GREATER_THAN_FUNCTION>
bool Test3(size_t szCount, double dLoadFactor, vector<KEY>& vVals,
           const HASH_FUNCTION& hf, const COMPARATOR_FUNCTION& cf,
           const GREATER_THAN_FUNCTION& gf);

// ...

Test3<value_type>(1000, 0.7f, vVals, sh, cf, gf);
0

精彩评论

暂无评论...
验证码 换一张
取 消