Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

edited body
Source Link
Andrew Truckle
  • 19.5k
  • 17
  • 94
  • 232

I have this example code

#include <iostream>
#include <array>
#include <bitset>
template <class T, std::size_t N>
class Booth
{
 public:
 int function(const std::array<T, N> & data) {
 std::bitset<data.size()> bit_set{};
 return 0;
 };
 private:
};
int main()
{
 std::cout << "std::array test\n";
 std::array<int, 3> data2{2};
 Booth<int, 3>booth{};
 std::cout << booth.function(data2) << std::endl;
 return 0;
}

It compiles fine in [OnlineGDB][1]. ANdAnd in the older versions of gcc. However, it started failing in gcc 11:

g++ -Wall -Wconversion -lstdc++ -pthread -std=c++17 -o array_size src/array_size.cpp
src/array_size.cpp: In function ‘int function(const std::array<int, 42>&)’:
src/array_size.cpp:22:28: error: ‘data’ is not a constant expression
 22 | std::bitset<data.size()> bit_set{};
 | ^
src/array_size.cpp:22:26: note: in template argument for type ‘long unsigned int’
 22 | std::bitset<data.size()> bit_set{};
 | ~~~~~~~~~^~

The code is the same. The language version is the same. I know how to fix this error. I just would like to know what was changed in the compiler? What is the reason for this change and thus the failure?

I have this example code

#include <iostream>
#include <array>
#include <bitset>
template <class T, std::size_t N>
class Booth
{
 public:
 int function(const std::array<T, N> & data) {
 std::bitset<data.size()> bit_set{};
 return 0;
 };
 private:
};
int main()
{
 std::cout << "std::array test\n";
 std::array<int, 3> data2{2};
 Booth<int, 3>booth{};
 std::cout << booth.function(data2) << std::endl;
 return 0;
}

It compiles fine in [OnlineGDB][1]. ANd in the older versions of gcc. However, it started failing in gcc 11:

g++ -Wall -Wconversion -lstdc++ -pthread -std=c++17 -o array_size src/array_size.cpp
src/array_size.cpp: In function ‘int function(const std::array<int, 42>&)’:
src/array_size.cpp:22:28: error: ‘data’ is not a constant expression
 22 | std::bitset<data.size()> bit_set{};
 | ^
src/array_size.cpp:22:26: note: in template argument for type ‘long unsigned int’
 22 | std::bitset<data.size()> bit_set{};
 | ~~~~~~~~~^~

The code is the same. The language version is the same. I know how to fix this error. I just would like to know what was changed in the compiler? What is the reason for this change and thus the failure?

I have this example code

#include <iostream>
#include <array>
#include <bitset>
template <class T, std::size_t N>
class Booth
{
 public:
 int function(const std::array<T, N> & data) {
 std::bitset<data.size()> bit_set{};
 return 0;
 };
 private:
};
int main()
{
 std::cout << "std::array test\n";
 std::array<int, 3> data2{2};
 Booth<int, 3>booth{};
 std::cout << booth.function(data2) << std::endl;
 return 0;
}

It compiles fine in [OnlineGDB][1]. And in the older versions of gcc. However, it started failing in gcc 11:

g++ -Wall -Wconversion -lstdc++ -pthread -std=c++17 -o array_size src/array_size.cpp
src/array_size.cpp: In function ‘int function(const std::array<int, 42>&)’:
src/array_size.cpp:22:28: error: ‘data’ is not a constant expression
 22 | std::bitset<data.size()> bit_set{};
 | ^
src/array_size.cpp:22:26: note: in template argument for type ‘long unsigned int’
 22 | std::bitset<data.size()> bit_set{};
 | ~~~~~~~~~^~

The code is the same. The language version is the same. I know how to fix this error. I just would like to know what was changed in the compiler? What is the reason for this change and thus the failure?

Source Link
ilya1725
  • 5.1k
  • 10
  • 49
  • 75

std::array:size() as a const in gcc 11

I have this example code

#include <iostream>
#include <array>
#include <bitset>
template <class T, std::size_t N>
class Booth
{
 public:
 int function(const std::array<T, N> & data) {
 std::bitset<data.size()> bit_set{};
 return 0;
 };
 private:
};
int main()
{
 std::cout << "std::array test\n";
 std::array<int, 3> data2{2};
 Booth<int, 3>booth{};
 std::cout << booth.function(data2) << std::endl;
 return 0;
}

It compiles fine in [OnlineGDB][1]. ANd in the older versions of gcc. However, it started failing in gcc 11:

g++ -Wall -Wconversion -lstdc++ -pthread -std=c++17 -o array_size src/array_size.cpp
src/array_size.cpp: In function ‘int function(const std::array<int, 42>&)’:
src/array_size.cpp:22:28: error: ‘data’ is not a constant expression
 22 | std::bitset<data.size()> bit_set{};
 | ^
src/array_size.cpp:22:26: note: in template argument for type ‘long unsigned int’
 22 | std::bitset<data.size()> bit_set{};
 | ~~~~~~~~~^~

The code is the same. The language version is the same. I know how to fix this error. I just would like to know what was changed in the compiler? What is the reason for this change and thus the failure?

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /