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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:04:28+00:00 2026-05-10T23:04:28+00:00

I want to align my member variables based on a class template type but

  • 0

I want to align my member variables based on a class template type but I’m not sure if it is actually possible.

The following is a (very) simple example of what I’d like to do

template<int Align> class MyClass { private:   struct MyStruct   {     // Some stuff   } __declspec(align(Align));    __declspec(align(Align)) int myAlignedVariable; }; 

So what I’d like is for Align to be a per-instance variable, and only through that is the align value of the class contents decided.

Unfortunately I always get the following error

error C2975: 'test::MyClass' : invalid template argument for 'Align', expected compile-time constant expression 

So, is this actually possible or can the alignment only be possible using a fixed compile time constant? If not, can anyone think of a way around this?

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. 2026-05-10T23:04:29+00:00Added an answer on May 10, 2026 at 11:04 pm

    Custom alignment isn’t in the standard, so how the compilers deal with it is up to them – looks like VC++ doesn’t like combining templates with __declspec.

    I suggest a work-around using specialisation, something like this:

    template<int A> struct aligned; template<> struct aligned<1> { } __declspec(align(1)); template<> struct aligned<2> { } __declspec(align(2)); template<> struct aligned<4> { } __declspec(align(4)); template<> struct aligned<8> { } __declspec(align(8)); template<> struct aligned<16> { } __declspec(align(16)); template<> struct aligned<32> { } __declspec(align(32)); 

    and then derive from that in your code:

    template<int Align> class MyClass { private:   struct MyStruct : aligned<Align> {     // stuff   }; }; 

    This unfortunately breaks the POD-ness of MyStruct. It also doesn’t work with built-in/existing types, so you’ll have to use a wrapper for those.

    aligned_t<int, 4> myAlignedVariable; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to align my text to the middle of my div, but I
I want to align my classes on the VS 2010 architect UML class diagram
I have - I think - a very simple problem I want to align
I want to center-align a GridView control in a <div> . But the GV
I have a couple of <p> tags that I want to right align. Does
I want to reference a COM DLL in a .NET project, but I also
CSS is not my strong suit, and I am not sure what's going on
I want to align some text to the top of a div. It seems
I want to align a Custom Button Field (the Star on the Screenshot) to
i want to align text box to right in cell of table. it is

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.