CC icon indicating copy to clipboard operation
CC copied to clipboard

No template support?

Open HarrievG opened this issue 4 months ago • 2 comments

I am trying to use cc with generic types.

i would expect there to be some way to make the following valid:


#define CC_NO_SHORT_NAMES
#include <https://raw.githubusercontent.com/JacksonAllan/CC/refs/heads/main/cc.h>

template< typename T> struct cc_tmpl_test
{
   cc_vec( T ) data;
};

int main()
{

cc_tmpl_test<float> s;

}

see https://godbolt.org/z/zfdx7zGqb

HarrievG avatar Sep 11 '25 23:09 HarrievG

Uhm, FOR C. that means, no templates D'uh!

HarrievG avatar Sep 12 '25 17:09 HarrievG

Hi HarrievG!

I was aware that CC containers don't interact well with C++ templates but hadn't considered it much of an issue because there's not much reason to use CC containers directly in C++ code, where we have access to templates. C++ compatibility mainly exists so that the library can easily be used in another C library that might itself be used in C++ projects. However, if only for the sake of completeness and robustness, I'd like to fix this issue in the next release anyway.

It seems that the main problem is the way that we're emulating typeof for expressions and types in C++:

#define CC_TYPEOF_XP( xp ) std::decay<std::remove_reference<decltype( xp )>::type>::type
#define CC_TYPEOF_TY( ty ) std::decay<std::remove_reference<decltype( std::declval<ty>() )>::type>::type

The first question that this code raises is why remove_reference is necessary when we're already using decay. remove_reference seems redundant, and I can't remember why I included it. The second problem is that these macro's don't work when the type or expression involves a template argument.

A cursory investigation reveals that these alternatives seem to work:

#define CC_TYPEOF_TY( ty ) typename std::decay<decltype(std::declval<ty>())>::type
#define CC_TYPEOF_XP( xp ) typename std::decay<decltype( xp )>::type

But I'll need to do more testing to ensure that this change doesn't lead to other problems and brush up on my modern C++ knowledge. I'd like to reopen this issue until I properly fix it.

JacksonAllan avatar Sep 13 '25 00:09 JacksonAllan