Modify

Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#4397 closed Feature Requests (fixed)

specialization for lexical_cas for identical Target and Source types

Reported by: alexey.kutumov@… Owned by: apolukhin
Milestone: Boost 1.44.0 Component: lexical_cast
Version: Boost 1.44.0 Severity: Optimization
Keywords: lexical_cast specialization Cc: alexey.kutumov@…

Description

std::string s1 = "str"; std::string result = boost::lexical_cast<std::string>(s1);

In this case it is possible to use specialization of lexical_cast which just returns input arg.

I created simple example with specialization of lexical_cast:

#include <boost/cstdint.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/type_traits.hpp>
#include <iostream>

template <typename Target, typename Source, bool val> struct helper
{
	/// Default action is to use lexical_cast
	static Target get(const Source & src)
	{
		return boost::lexical_cast<Target>(src);
	}
};

template <typename Target, typename Source> struct helper<Target, Source, true>
{

	/// Use this if Target and Source identical
	static Target get(const Source & src)
	{
		return src;
	}
};

template <typename Target, typename Source> Target my_lexical_cast(const Source & res)
{
	/// Use boost::is_same for checking identity of Target and Source
	return helper<Target, Source, boost::is_same<Target, Source>::value>::get(res);
}

int main(int argc, char * argv[])
{
	std::string b1 = "10";

	std::string b2 = my_lexical_cast<std::string>(b1);
	int b3 = my_lexical_cast<int>(b1);

	std::cout << "b1: " << b1 << std::endl;
	std::cout << "b2: " << b2 << std::endl;
	std::cout << "b3: " << b3 << std::endl;

	return 0;
}

This code checked in msvc 9.0, mingw32-g++ 4.4.0

Attachments (1)

conv_test.cpp (2.3 KB) - added by alexey.kutumov@… 7 years ago.
Simple test for performance of specialization of lexical_cast

Download all attachments as: .zip

Change History (5)

Changed 7 years ago by alexey.kutumov@…

Simple test for performance of specialization of lexical_cast

comment:1 Changed 7 years ago by alexey.kutumov@…

I compiled given test under msvc 2008, and my_lexical_cast is two times faster then usual boost::lexical_cast

comment:2 Changed 6 years ago by apolukhin

  • Owner changed from nasonov to apolukhin
  • Status changed from new to assigned

comment:3 Changed 6 years ago by apolukhin

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [71958]) Fixes #5350. Fixes #4397 More tests (for conversions to float types, for conversions of negative values to unsigned integers)

Last edited 6 years ago by apolukhin (previous) (diff)

comment:4 Changed 6 years ago by apolukhin

Thanks for the idea.
It was redesigned and implemented in #5350, #5350 was merged to trunk (with some fixes for behavior described in #5494 and more optimizations for conversions from arithmetic to arithmetic types).

Last edited 6 years ago by apolukhin (previous) (diff)

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain apolukhin.
The resolution will be deleted. Next status will be 'reopened'.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.