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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:52:48+00:00 2026-05-18T03:52:48+00:00

Assume the following toy class, and modern compilers (recent gcc for example). template <typename

  • 0

Assume the following toy class, and modern compilers (recent gcc for example).

template <typename T>
class SomeVec {
public:
  ...
  virtual T get(const int index) = 0;
}

The application involves a fair amount of number crunching based on values stored in SomeVec subclasses, T being a primitive type, say int. However, the practice of stl containers and boost::numeric::ublas::vector is to return stored values via (const) reference.

I wondered the performance differences this involve. In this question it is shown that array element access by value and vector element access by reference results in the same code, so I assume the compiler can in some cases optimize stuff away.

Now my questions are:

  • (1) stl and ublas are templated, while my solution requires virtual methods. Does this hinder modern compilers’ ability to optimize code?

  • (2) If the compiler could not optimize to return the const atomic reference as value, then do I assume it right that the virtual method call and the dereferencing cost approximately the same? Or is one significantly more expensive than the other?

Thanks!

  • 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-18T03:52:48+00:00Added an answer on May 18, 2026 at 3:52 am

    The reason STL returns references is because templated code doesn’t have the luxury of knowing that returned objects are small. While an int is no problem, returning a large struct slows things down for no good reason. In the latter case it only makes sense to use references, and since a reasonable compiler can optimise the case of using primitive types you may as well use references throughout.

    Note that your method virtual T get(const int index) differs in other ways to STL container methods. Most importantly, and related to the issue above, your method returns a copy of the indexed object: manipulating the result will not change the state of the object in your container.

    Also, declaring the index argument as const does nothing, since you pass index in by value and so all you are doing is preventing yourself from changing index locally within implementation. If you were passing index in by reference that would be a different matter, but you should be wary of doing so.

    Finally, are you really sure that your class needs to be dynamically polymorphic (i.e., have virtual methods)? The STL containers are intentionally designed not to be inherited (which is why they do not have virtual destructors). Containers are not meant to provide an interface for derived classes, rather they are there to facilitate an implementation. I would argue that the subclass examples you suggest could just as easily be implemented as wrapper classes around a templated container, favouring code reuse through composition over inheritance (something advocated by the Gang of Four, amongst others). Aside from being good practice, avoiding virtual methods saves having vtables and corresponding pointers in your objects, and requiring the extra vtable lookup in each call. If you don’t really need dynamic polymorphism, why take the cost (and possibly prevent compiler optimisations)?

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

Sidebar

Related Questions

Assume the following code: public class Foo { public string Bar { get; set;
Assume the following entity classes: public class Player { public virtual int ID {
Lets assume following classes definition: public class A { public final static String SOME_VALUE;
In C++, assume following class hierarchy: class BaseClass { }; class ChildClass : public
Assume following: public class MyFunkyTable : DbObject { // this class will be generated
Let's assume following code snippet: public class NotThatWellWrittenClass { public static void doSmth() {
assume the following class is given: class Base{ public: Base() {} Base( const Base&
Assume the following simple case (notice the location of virtual ) class A {
Assume the following class: public class MyEnum: IEnumerator { private List<SomeObject> _myList = new
Assume the following type definitions: public interface IFoo<T> : IBar<T> {} public class Foo<T>

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.