About Store Forum Documentation Contact



Post Reply 
Partial Template Specialization Bug
Author Message
TBJokers Offline
Member

Post: #1
Partial Template Specialization Bug
In C++ if you were to write a class just as an example:

Code:
enum CharacterClass
{
    Fist = 1 << 0,
    Blade = 1 << 1,
    RedMage = 1 << 2,
    Archer = 1 << 3,
    BlueMage = 1 << 4,
};
template<CharacterClass ChrClass = Fist>
struct StartUp
{
    static void GiveStats(Player &plr)
    {
        plr.Damage += 300;
    }
};

template<>
struct StartUp<Blade>
{
    static void GiveStats(Player &plr)
    {
        plr.Damage += 400;
    }
};

We'd know that we could call:
Code:
StartUp<Fist>::GiveStats(Character);
To get 300 more Damage as noted in this example.

However when compiling this code with the code editor it first generates
Code:
template<CharacterClass = Fist>
struct StartUp;
template<>
struct StartUp;
Where as our argument list just got missing!

Then it'd also compile something like this into header files!
Code:
template<>
struct StartUp<Blade>
{
    static void GiveStats(Player &plr);
};
template<CharacterClass ChrClass >
struct StartUp
{
    static void GiveStats(Player &plr);

public:
    StartUp();
};
template<CharacterClass ChrClass = Fist>
void StartUp<ChrClass>::GiveStats(Player &plr)
{
   plr.Damage += 300;
}
template<CharacterClass ChrClass = Fist>
StartUp<ChrClass>::StartUp() : ChrClass(Fist) {}

And in a Cpp file this
Code:
void Unique StartUp 256677E4::GiveStats(Player &plr)
{
      plr.Damage += 400;
}
Well, now we can start losing track of how many errors there possibly are in the compiled code..

This COULD work if it was changed around abit..
Code:
//Top Of @@Headers.h
template<CharacterClass = Fist>
struct StartUp;
template<>
struct StartUp<Blade>;

//SeperateHeaderFile
template<CharacterClass ChrClass >
struct StartUp
{
    static void GiveStats(Player &plr);

public:
    StartUp();
};

//SeperateHeaderFile
template<>
struct StartUp<Blade>
{
    static void GiveStats(Player &plr);
};
So all we did in our Header files was to add argument list and some few things were deleted (Constructor body and GiveStats function)

instead we make a small few changes to those, and add those into cpp file.
Code:
void StartUp<Fist>::GiveStats(Player &plr)
{
      plr.Damage += 400;
}

void StartUp<Blade>::GiveStats(Player &plr)
{
      plr.Damage += 400;
}

StartUp<Fist>::StartUp() {}
You could either do that.. Or for partial template specialization that you include *that/those* header files last and keeping it 100% c++ style and still keeping..
Code:
//Top Of @@Headers.h
template<CharacterClass = Fist>
struct StartUp;
template<>
struct StartUp<Blade>;
But I could imagine that you've never thought about partial template specialization but it's a very nice feature and i'm using templates commonly and many systems that i've built standalone that I am importing are using just this method.

Now this all being said there's the argument list missing, and also redefinitions of default parameters. There's the constructor with an initialization list for the default argument?? and there are incorrect function names.

Now I'm not sure that this is a bug as i mentioned, but i'd highly appreciate it being added/fixed!

Note never tried partial template specialization using polymorphism and virtual base classes etc.

Best Regards TBJokers.
02-26-2015 04:03 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Partial Template Specialization Bug
Hi,

Sorry, template specialization isn't currently supported from the Code Editor, it's a bit tricky to do.
Regular templates work fine, but the specialization part is the issue. I have to recommend trying some workaround.
02-26-2015 11:32 PM
Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #3
RE: Partial Template Specialization Bug
Greg, maybe there is an option to add in-editor macros to define a code that shall not be modified when compiling?

Something like this:
Code:
>>EE_H_START<<
unmodified header code here
>>EE_H_END<<

Code:
>>EE_CPP_START<<
unmodified cpp code here
>>EE_CPP_END<<
02-27-2015 08:09 AM
Visit this user's website Find all posts by this user Quote this message in a reply
TBJokers Offline
Member

Post: #4
RE: Partial Template Specialization Bug
(02-27-2015 08:09 AM)Houge Wrote:  Greg, maybe there is an option to add in-editor macros to define a code that shall not be modified when compiling?

Something like this:
Code:
>>EE_H_START<<
unmodified header code here
>>EE_H_END<<

Code:
>>EE_CPP_START<<
unmodified cpp code here
>>EE_CPP_END<<

That is actually something I could work with, it's similar to the Unreal Scripts.

Esenthel? smile
02-27-2015 05:18 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #5
RE: Partial Template Specialization Bug
(02-27-2015 08:01 PM)aceio76 Wrote:  I personally am not fond of deviating from the c/c++ confines and adding tags that isn't recognizable within the language.

I fully agree, but

(02-26-2015 11:32 PM)Esenthel Wrote:  template specialization isn't currently supported from the Code Editor

Also

(02-27-2015 08:01 PM)aceio76 Wrote:  Don't forget that the code you put in CE is not a script language, but a true C++ language where CE does 'extra things' to make it faster for us to code and maintain, all in an effort to be able to develop games faster.

That's true, but

(02-26-2015 11:32 PM)Esenthel Wrote:  it's a bit tricky to do.

And of course

(02-27-2015 08:01 PM)aceio76 Wrote:  The better approach, IMO, is to fix the template specialization and maintain how it would look from a c++ perspective.

The point is that

(02-26-2015 11:32 PM)Esenthel Wrote:  I have to recommend trying some workaround.

And tags are like a workaround.
(This post was last modified: 02-27-2015 08:16 PM by Houge.)
02-27-2015 08:16 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: Partial Template Specialization Bug
You can always include raw c++ code in a regular *.h file, and include that file in EE Application Element properties.
Or rework your code to don't use template specialization.
02-27-2015 09:43 PM
Find all posts by this user Quote this message in a reply
TBJokers Offline
Member

Post: #7
RE: Partial Template Specialization Bug
(02-27-2015 08:01 PM)aceio76 Wrote:  I personally am not fond of deviating from the c/c++ confines and adding tags that isn't recognizable within the language. It's already hard enough to keep updated with C++14 and more upcoming and those features that have yet to be integrated with our CE. Don't forget that the code you put in CE is not a script language, but a true C++ language where CE does 'extra things' to make it faster for us to code and maintain, all in an effort to be able to develop games faster.
The better approach, IMO, is to fix the template specialization and maintain how it would look from a c++ perspective.

Also, TBJokers, based on your example (and I don't know your exact use-case or use-cases) but it seems that you can simply rewrite your code to not have to rely on templates that way. It doesn't look like you really benefit (performance or otherwise) with how your code is using template specialization (unless I am missing something that I didn't recognize).
I cannot rewrite my code and keep the performance nor same simple structure of the systems.

Note including c++ header file will make it read only and small buggs could turn into a real time waster as you would have to open up Visual studio and modify the code. (Works fine for me so far but could I imagine)
02-27-2015 10:23 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply