| 
 | 
 | 
 
Home » U++ Library support » U++ Core » Doubt with Buffer<> of a trivially destructible type 
	
		
		
			| Doubt with Buffer<> of a trivially destructible type [message #61220] | 
			Thu, 12 December 2024 12:58   | 
		 
		
			
				
				
				  | 
					
						  
						koldo
						 Messages: 3453 Registered: August 2008 
						
					 | 
					Senior Veteran  | 
					 | 
		 
		 
	 | 
 
	
		Hello all 
 
Compiling this code with CLANG, I get the error: "object expression of non-scalar type 'double[3]' cannot be used in a pseudo-destructor expression" in the line with Buffer. 
However, vector3_destructible gets true. 
I wanted to ask you how to use Buffer for such a data type, or if there is a better dynamic container. 
 
typedef double Vector3[3];
bool vector3_destructible = std::is_trivially_destructible<Vector3>::value;
Buffer<Vector3> vector3_data(10);  
		
		
  Best regards 
Iñaki
		
 |  
	| 
		
	 | 
 
 
 |  
	
		
		
			| Re: Doubt with Buffer<> of a trivially destructible type [message #61221 is a reply to message #61220] | 
			Thu, 12 December 2024 14:15    | 
		 
		
			
				
				
				
					
						  
						Oblivion
						 Messages: 1241 Registered: August 2007 
						
					 | 
					Senior Contributor  | 
					 | 
		 
		 
	 | 
 
	
		Hello Iñaki, 
 
Quote: 
However, vector3_destructible gets true. 
  
 
Yes, because std::is_trivially_destructible removes all extents (dimensions) of the object in question before its type gets evaluated. So, basically it is returning true for double type. 
 
However, Buffer<Vector3> doesn't. So it expands into Buffer<double[3]>, hence the error. 
 
I think you should instead prefer -if possible- a structure or Buffer<double> mybuffer(count * 3). 
 
Then again, I don't know the exact requirements of your code. 
 
 
Best regards, 
Oblivion 
		
		
  Github page: https://github.com/ismail-yilmaz 
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
		[Updated on: Thu, 12 December 2024 14:17] Report message to a moderator  
 |  
	| 
		
	 | 
 
 
 |  
	
		
		
			| Re: Doubt with Buffer<> of a trivially destructible type [message #61223 is a reply to message #61220] | 
			Thu, 12 December 2024 14:40    | 
		 
		
			
				
				
				
					
						  
						Oblivion
						 Messages: 1241 Registered: August 2007 
						
					 | 
					Senior Contributor  | 
					 | 
		 
		 
	 | 
 
	
		However, this should work too: 
 
CONSOLE_APP_MAIN
{
	typedef std::array<double, 3> Vector3;
	Buffer<Vector3> vector3_data(10, {1.0, 2.0, 3.0});
	for(int i = 0; i < 10; i++) {
		auto a = vector3_data[i];
		Cout() << a[0] << " "<<  a[1] << " " << a[2] << "\r\n";
	}
	
}
 
 
Or, C++17 style: 
 
	typedef std::array<double, 3> Vector3;
	Vector<Vector3> vector3_data(10, {1.0, 2.0, 3.0});
	for(auto& [a, b, c] : vector3_data) {
		Cout() << a << " "<<  b << " " << c << "\r\n";
	}
	
 
 
 
Best regards, 
Oblivion
		
		
  Github page: https://github.com/ismail-yilmaz 
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
		[Updated on: Thu, 12 December 2024 14:43] Report message to a moderator  
 |  
	| 
		
	 | 
 
 
 |  
	| 
		
 |  
	
		
		
			| Re: Doubt with Buffer<> of a trivially destructible type [message #61243 is a reply to message #61224] | 
			Sat, 14 December 2024 19:19    | 
		 
		
			
				
				
				
					
						  
						Lance
						 Messages: 656 Registered: March 2007 
						
					 | 
					Contributor   | 
					 | 
		 
		 
	 | 
 
	
		This is a valid use case, and something need to be addressed in u++ libary directly instead of circling around. 
 
Here is a simple utility we can use to fix it from with u++ libary 
 
template <typename T, std::size_t...>
constexpr auto object_count(T& t)
{
	return 1u;
}
template <std::size_t ... Ns, typename T, std::size_t n>
constexpr auto object_count(T (&arr)[n] )
{
	return n * object_count(arr[0]);
}
   // eg, with
    double d;
    double a1[5];
    double a2[5][3];
    double a3[5][4][2];
    // then
    static_assert(object_count(d)==1,"?");
    static_assert(object_count(a1)==5,"?");
    static_assert(object_count(a2)==15,"?");
    static_assert(object_count(d3)==40,"?);
 
 
With above utility, we can easily modify u++ Vector to accomodate c style array. 
 
basially, if T is trivially relocatible, then 
any c array of T is alos trivially relocatible, 
Upp::Vector don't care any detail of c array, except the total number of T objects in the array to properly construct, move and destruct it, with T::~T() properly defined of course. 
 
Oh, for 
     // some type T
     T d3[3][2][5];
 
 
a simple 
 
will do the job  
		
		
		
 |  
	| 
		
	 | 
 
 
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	
		
		
			| Re: Doubt with Buffer<> of a trivially destructible type [message #61338 is a reply to message #61331] | 
			Fri, 27 December 2024 18:34   | 
		 
		
			
				
				
				  | 
					
						  
						koldo
						 Messages: 3453 Registered: August 2008 
						
					 | 
					Senior Veteran  | 
					 | 
		 
		 
	 | 
 
	
		mirek wrote on Thu, 26 December 2024 18:47-  For C++ 17, just insert constexpr in the std::is_trivially_destructible check, to force it to work in compile time: 
if constexpr (std::is_trivially_destructible<T>::value)  
 
We are now C++17 -> used this one Thank you Mirek. Problem solved! 
		
		
  Best regards 
Iñaki
		
 |  
	| 
		
	 | 
 
 
 |   
Goto Forum:
 
 Current Time: Tue Nov 04 17:09:23 CET 2025 
 Total time taken to generate the page: 0.05307 seconds 
 |   
 |  
  |