Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:28:42+00:00 2026-05-28T14:28:42+00:00

I am currently involved embedded C++ development on am STM32 platform. Our team is

  • 0

I am currently involved embedded C++ development on am STM32 platform. Our team is evaluating the use of templates to parametrize drivers for various low-level hardware devices.

All valid template specializations are known in advance thus we can give all valid specializations explicitly inside a implementation file (implementation and declaration separated). In fact, to us explicit specialization is quite useful since it helps to document viable parameter sets.

// file i2c_dev.h

template<typename traits>
struct i2c_dev
{
public:
   static void init();
   static void send();
   static bool busy();
   ...
private:
   static i2c_transfer periodic_transfer; // class with used-defined constructor
};

// traits class for configuration A
struct i2c_dev_traitsA
{
   enum 
   { 
       I2Cx_BASE    = I2C1_BASE
     , PORTx_BASE   = GPIOB_BASE
     , PORTx_PIN_TX = PB08
     , PORTx_PIN_RX = PB09
   };
};

// traits class for configuration B, different I2C peripherial and pinout
struct i2c_dev_traitsB
{
   enum 
   { 
       I2Cx_BASE    = I2C2_BASE
     , PORTx_BASE   = GPIOA_BASE
     , PORTx_PIN_TX = PA01
     , PORTx_PIN_RX = PA02
   };
};

// file i2c_dev.cpp

// Implementation of template functions
template<typename traits> 
void i2c_devy<traits>::init() { ... }

...

// explcitly specialize for all valid traits classes
template class i2c_dev<i2c_dev_traitsA>;
template class i2c_dev<i2c_dev_traitsB>;

Even though generally only one of the specializations will be actually used, the code generated for unused specializations gets removed from the final image by the linker, which is exactly what we want.

However, the static member variables – periodic_transfer in the above sample – of each template specialization remain in the executable, as can be seen in the memory map generated by the arm-none-eabi-nm tool. This is probably due to the fact that i2c_transfer is not a POD but has a user-defined constructor. When the constructor is removed, turning the thing into a POD type, the static members also vanish.

Is there a way to remove static non-POD members of explicitly instantiated, but unused templates?

Regards,
Arne

Edit #1: After rethinking the problem I came up with the following solution, which apparently fixes the problem.

When class i2c_transfer which in fact has it’s constructors only for clarity and ease of use, has its data members moved into a POD base class i2c_transfer_pod like this:

 struct i2c_transfer_pod
 {
 protected:
    uint16_t m_size;
    char* m_buffer;
 };

 struct i2c_transfer : public i2c_transfer_pod
 {
 public:
    i2c_transfer();
    i2c_transfer(i2c_direction_enum dir, char*buffer, uint16_t count);

    bool failed();
    bool succeeded();
 };

Then, the static members of unused i2c_dev<traits> specializations are also removed from the final executable (as the map file suggests).

Edit #2: Although answering one-self feels a bit lame.. I would kindly request comments on the proposed solution. Is there possibly a more elegant way? Is the compiler really (as I presume) optimizing away the additional derivation?

Edit #3: I close the question since the solution works for me. Would be nice to have deeper insight into the reason for the observed behaviour.

The compiler in question is arm-none-eabi-gcc (Sourcery G++ Lite 2011.03-42) 4.5.2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:28:43+00:00Added an answer on May 28, 2026 at 2:28 pm

    Answer (from Edit #1): After rethinking the problem I came up with the following solution, which apparently fixes the problem.

    When class i2c_transfer which in fact has it’s constructors only for clarity and ease of use, has its data members moved into a POD base class i2c_transfer_pod like this:

     struct i2c_transfer_pod
     {
     protected:
        uint16_t m_size;
        char* m_buffer;
     };
    
     struct i2c_transfer : public i2c_transfer_pod
     {
     public:
        i2c_transfer();
        i2c_transfer(i2c_direction_enum dir, char*buffer, uint16_t count);
    
        bool failed();
        bool succeeded();
     };
    

    Then, the static members of unused i2c_dev<traits> specializations are also removed from the final executable (as the map file suggests).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently involved in the development of a large rails application that interfaces
I am currently involved in the development of a software using distributed computing to
First of all thanks to folks who are currently involved in the development of
We currently use the DirectShow API to stream standard definition video into our application
I'm a software test engineer embedded in a development team. A large part of
I'm currently working on a project involved in deploying Windows 7 (configured to our
I am currently involved in developing a low level network application. We have lately
I'm currently involved in a heavily localised WPF application and we've had a bug
I'm currently involved in a research project that requires me to access a Windows
I'm currently involved with developing a C# CMS-like web application which will be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.