0

I have a template class as follows:

template <class T, int N>
class kernel: public std::array<T, N << 1> {
    :
public:
    kernel(void) : std::array<T, N << 1>{} { blah... }
    template <typename... E>
    kernel(const E &&... e):
        std::array<T, N << 1>{{std::forward<const E>(e)...}} { blah... }
    :
    T write_wav(const char *filename) const { blah... }
};

In my program, I instantiate kernel<T, N> for various values of T and N without problems, but never actually call kernel<T, N>::write_wav(), and so the compiler does not generate a definition for it.

Thing is, I WANT the compiler to generate a definition, because I want to call kernel<T, N>::write_wav() from my debugger (Immediate Window in VC++2015). How would I force the compiler to do so?

Thus far, the only working approach I stumbled upon is to call kernel<T, N>::write_wav() from the constructor, and hence generate a reference to it:

kernel(void) : std::array<T, N << 1>{} { write_wav(nullptr); }
template <typename... E>
kernel(const E &&... e):
    std::array<T, N << 1>{{std::forward<const E>(e)...}} { write_wav(nullptr); }

Alas, this introduces an unnecessary execution overhead. Is there a cleaner way to do this?

If I try taking the address of kernel::write_wav(), like so:

    kernel(void) : std::array<T, N << 1>{} { &kernel<T, N>::write_wav; }

Then things compile fine but the member function is still not instantiated (probably because the compiler can figure out that its address has no uses).

Alternately, I tried creating a pointer to member inside the class and assigning the address to it:

    T(kernel<T, N>::*write_wav_ptr)(const char *filename) const;
    kernel(void) : std::array<T, N << 1>{} { write_wav_ptr = &kernel<T, N>::write_wav; }

Then things work fine (and kernel::write_wav() gets instantiated), but each instance of kernel now has an extra data member that I don't really need.

zll
  • 91
  • 1
  • 10
  • 1
    Is `write_wave` the same thing you refer to as `output_wave`? The compiler should generate a definition for it for any given `kernel` which has been instantiated. – David Aug 10 '16 at 19:33
  • 2
    Take the address of it – Richard Critten Aug 10 '16 at 19:38
  • Thank you all for your feedback. I've updated my state of knowledge in the description above. The take-away is that assigning the address of the member function to a data member does the trick -- at the expense of making each instance of the class slightly bigger -- so this is still not ideal. – zll Aug 12 '16 at 13:14

0 Answers0