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 8874299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:41:06+00:00 2026-06-14T18:41:06+00:00

Consider the exponential smoother template class below. This class is for smoothing/filtering sequential data

  • 0

Consider the exponential smoother template class below. This class is for smoothing/filtering sequential data exponentially (see update method). Elemtype might be an vector and Floattype is usually a scalar. E.g.

ExponentialSmoother<Eigen::Vector2f, float> x(0.1, Vector2f(0.5, 0.5));

In this example the second template parameter Floattype could be avoided because Eigen’s Matrix class contains a nested typedef to get the scalar base type:

Vector2f::Scalar

It is also reasonable to instantiate both Elemtype and Floatype as floats to smooth one dimensional data. In this case the second template paramater could also be skipped.

template <class Elemtype, class Floattype>
class ExponentialSmoother
{
public:
    // ctor
    ExponentialSmoother(Floattype alpha, Elemtype& initial_estimate);

    // getters
    inline const Elemtype& getValue() const {return estimate_;}
    inline const Floattype getAlpha() const {return alpha_;}

    const Elemtype& update(const Elemtype& curr)
    {
       estimate_ = (alpha_ * curr) + (((Floattype)1-alpha) * estimate_);
       return estimate_;
    }

private:
    Elemtype estimate_;
    Floattype alpha_;  // smoothing factor within [0,1]
}

Now my question is what is the “most elegant” solution to implement the ExponentialSmoother with only one template parameter (the element type)?
It should work with Eigen vectors and matrices but also with floating point types.

In other words, is it possible to check if Elemtype::Scalar exists and if not (i.e. Elemtype is float or double) define the Floattype as Elemtype?

A similar question has been asked here. But I am wondering what the most generic solution is if for instance STL vectors should be supported as well. Would all types require the same nested typedef (or some traits class with consistent naming)?

  • 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-06-14T18:41:07+00:00Added an answer on June 14, 2026 at 6:41 pm

    You can use a helper. The link you gave almost contains the solution:

    template<class T, class R = void>  
    struct enable_if_type
    {
        typedef R type;
    };
    
    template<class E, class Enable = void>
    struct GetFloatType
    {
        typedef E type;
    };
    
    template<class E>
    struct GetFloatType<E, typename enable_if_type<typename E::Scalar>::type>
    {
        typedef typename E::Scalar type;
    };
    

    Then, in your class:

    template <class Elemtype, class Floattype = typename GetFloatType<Elemtype>::type>
    class ExponentialSmoother
    {
        // ...
    };
    

    Also, with this users can still manually provide their float type. You can see it live. Bonus: works with C++03 without problems.

    Note that you can add more partial specializations of GetFloatType. Here is a live example. Don´t forget that ElemType have to be acceptable for only one specialization of GetFloatType, or else it will be ambiguous (and cause a compiler error).

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

Sidebar

Related Questions

Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the code below. I do not want to create multiple instances of class
Consider this example - I have a class called Report that has a field
Consider this simple class: package net.zomis.test; public class Test { public Test() { }
Consider the FetchData method below. It is designed to avoid duplicating the database query
Consider the console application below, featuring a method with a generic catch handler that
Lets consider a simple class template< typename T > class Wrapper { public: //
consider the below class public class Player { private int id; public int getId()
Consider this simplified model in Django: class Item(models.Model): title = models.CharField(max_length=200) pub_date = models.DateTimeField()
Consider this code: #include <iostream> #include <type_traits> using namespace std; template<typename T_orig> void f(T_orig&

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.