Jump to content
  • Advertisement
Sign in to follow this  
Skarsha

Unity Specialisation of template parameter as another template class.

This topic is 4456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. In a previous question (Templates, Namespaces, Linking.) I asked about explicit template specialisation for a unique class. However, I now need to use specialisation where the explicit parameter is itself a template class. Description: template class CZug<T> contains a function bam() that calls template function bar<T>(). template function foo<T>() is declared but not defined except via specialisation. template function bar<T>(), by default calls template function foo<T>(). but bar<T>(), when T = template class CMup<T>, does nothing, thus avoiding the need to define foo<CMup<T> >(). Thus calling bam() on a CZug<CMup<int> > should compile. Unfortunately, I can't deduce the syntax for explicit specialisation using a template. Do I still use template <>?
namespace a {
    namespace b {

        template <typename T> void foo(T item);

    }
}

namespace a {
    namespace c {

        template <typename T> inline void bar(T item);

    }
}

template <typename T>
inline void a::c::bar(T item) {
    a::b::foo(item);
}


namespace a {

    template <typename T>
    class CZug {
      public:
        T m_item;
        void bam();
    };

}


template <typename T>
void a::CZug<T>::bam() {
    a::c::bar(m_item);
}



namespace a {
    namespace d {

        template <typename T>
        class CMup {
          public:
            T m_item;
        };

    }

    namespace c {

        //--------------------------------------------------------//
        // THE PROBLEM IS HERE: YOU CAN POSSIBLY IGNORE THE REST. //
        //--------------------------------------------------------//

        //template <> // error: too many template-parameter-lists.
        template <typename T>
        //template <> // invalid explicit specialization before '>' token.
        inline void bar(d::CMup<T> item);

    }
}

// template <>.
template <typename T>
// template <>.
inline void a::c::bar(a::d::CMup<T> item) { // <-- THIS FUNCTION SHOULD BE CALLED.
    // Do Nothing.
}

// Test routine.
int main() {
    a::CZug<a::d::CMup<int> > test;

    test.bam();
}

COMPILER ERROR: <code>void a::c::bar<a::d::CMup<int> >(a::d::CMup<int>*)]+0x11): undefined reference to `void a::b::foo<a::d::CMup<int> >(a::d::CMup<int>&)'</code> Any idea how I should write the declaration of void a::c::bar(d::CMup<T> item)?? NOTE: Again with the namespaces: These exist in the code I am writing (with different names of course). But if I remove them on the test code it compiles fine. ENVIRONMENT: Compiled using "GNU GCC Compiler" from "Code::Blocks" on Ubuntu (Breezy Badger) Linux, AMD64 version. Thanks for all assistance.

Share this post


Link to post
Share on other sites
Advertisement
Seems like an instantiation issue due to having CZug's implementation before the partial specialization you want it to use. This was discovered by accident simply simplifying your code to get rid of this gawdy "namespace a { ... } namespace a { ... } namespace a { ... }" junk.

Observe that commenting out the "#define DO_SPECIAL_AFTER" line will cause this code to work correctly (note: I have placed spaces after the backslashes to prevent the source tags from eating them up - remove the spaces to compile!):

#define SPECIAL_STUFF                     \ 
template <typename T> \
inline void bar(d::CMup<T> item); \
template <typename T> \
inline void bar(a::d::CMup<T> item) {}

#define DO_SPECIAL_AFTER

namespace a {
namespace b {
template <typename T> void foo(T item);
}
namespace d {
template <typename T>
class CMup {
public:
T m_item;
};
}
namespace c {
template <typename T> inline void bar(T item);
template <typename T>
inline void bar(T item) {
a::b::foo(item);
}
#ifndef DO_SPECIAL_AFTER
SPECIAL_STUFF
#endif
}
template <typename T>
class CZug {
public:
T m_item;
void bam();
};
template <typename T>
void CZug<T>::bam() {
a::c::bar(m_item);
}
namespace c {
#ifdef DO_SPECIAL_AFTER
SPECIAL_STUFF
#endif
}
}


// Test routine.
int main() {
a::CZug<a::d::CMup<int> > test;

test.bam();
}


Share this post


Link to post
Share on other sites
Thanks.

Seems odd, I reckon you should be able to do this properly somehow.

I just decided to make CMup non-template and use a cast. Might look back later and see if I can fix it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!