Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys,

For the below code, when I am running static code analysis, I'm getting the "'alignas' attribute cannot be applied to types" warning. How should I fix this warning? Please let me know if missing anything...

C++
template<size_t n, size_t a = alignof(size_t)>
struct test
{   
    struct test2
    {
        uint8_t d[n] alignas(a);
    };
};


Thanks in advance!!!

What I have tried:

I referred all articles regarding this, but only minimal information is exist apart from the concept of Memory Alignment.
Posted
Updated 6-Dec-21 1:40am
v2
Comments
11917640 Member 6-Dec-21 8:59am    
cppreference contains similar example: alignas(64) char cacheline[64]; Did you try to set alignas in the beginning?
Piraisudan 6-Dec-21 9:19am    
Hi @User-11884187Watch, thanks for the hint...

I changed from "uint8_t d[n] alignas(a)" to "alignas(a) uint8_t d[n]", it seems the warning got resolved.

Let me comeback, if anything.

Thanks!!!
Stefan_Lang 7-Dec-21 9:46am    
This seems a bit odd, but I remember compilers behaving oddly around any kind of template arguments other than types or int - unsigned int or size_t caused trouble or even compilation errors. Have you tried int a instead of size_t?
Piraisudan 7-Dec-21 10:18am    
Hi @Stefan_Lang, thank you for the input, let me check with int.

Meanwhile, could you please explain little bit more on this. I'm still couldn't understand what exactly happening.

Thanks!!!
Stefan_Lang 7-Dec-21 11:28am    
I can offer precious little more info, only that with some versions of C++ compilers I've had difficulty when using specific numeric type as template arguments.

Typically the cause of the problem was a template instantiation with an integral literal that was then interpreted as an int, whereas the template used some other type, such as unsigned int, long, or char.

In such cases, the error messages were typically not very helpful. The fastest way to find out if it was related to the template specification, was change the integral type to int: if that resolved the problem, it meant I needed to find a wonky template instantiation that accidentally instantiates with the wrong integral type.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900