Input strings in VC++ 2012
I have a source code
Code:
listener.support(methods::GET, [](http_request req)
{
std::cout << "Serving GET" << std::endl;
//req.reply(status_codes::OK, U("input string!"), U("text/html"));
req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a GET Request.)</body></html>"), U("text/html"));
});
Now I want to take some input from the user and then post it in req.reply. For that I tried this
Code:
std::string strName;
std::cin >> strName;
req.reply(status_codes::OK, strName, U("text/html"));
But somehow the compiler throws this error.
Quote:
1>Listener.cpp(36): error C3493: 'strName' cannot be implicitly captured because no default capture mode has been specified
1>Listener.cpp(36): error C2664: 'pplx::task<void> web::http::http_request::reply(web::http::status_code,const utility::string_t &,utility::string_t) const' : cannot convert parameter 2 from 'std::string' to 'const utility::string_t &'
1> Reason: cannot convert from 'std::string' to 'const utility::string_t'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Re: Input strings in VC++ 2012
See my reply in your another thread (about possible conversion)
Re: Input strings in VC++ 2012
[ off-topic ]
Probably, we have to ask for a new forum section named "VC++ 2012 & C++ REST SDK (codename Casablanca)". :)
[ on-topic ]
Have a look in ...\casablanca\include\cpprest\basic_types.h and you'll find
Code:
namespace utility
{
// ...
#ifdef _UTF16_STRINGS
//
// On Windows, all strings are wide
//
typedef wchar_t char_t ;
typedef std::wstring string_t;
#define _XPLATSTR(x) L ## x
typedef std::wostringstream ostringstream_t;
typedef std::wofstream ofstream_t;
typedef std::wostream ostream_t;
typedef std::wistream istream_t;
typedef std::wifstream ifstream_t;
typedef std::wistringstream istringstream_t;
typedef std::wstringstream stringstream_t;
#define ucout std::wcout
#define ucin std::wcin
#define ucerr std::wcerr
#else
//
// On POSIX platforms, all strings are narrow
//
typedef char char_t;
typedef std::string string_t;
#define _XPLATSTR(x) x
typedef std::ostringstream ostringstream_t;
typedef std::ofstream ofstream_t;
typedef std::ostream ostream_t;
typedef std::istream istream_t;
typedef std::ifstream ifstream_t;
typedef std::istringstream istringstream_t;
typedef std::stringstream stringstream_t;
#define ucout std::cout
#define ucin std::cin
#define ucerr std::cerr
#endif // endif _UTF16_STRINGS
// ...
}// namespace utility
So, to avoid conversion errors (and make cross-platform code) when deal with C++ REST SDK, use C++ REST SDK types: utility::string_t instead of std::string, utility::ucin and not std::cin, and so on.
Re: Input strings in VC++ 2012
Quote:
Originally Posted by
ovidiucucu
[ off-topic ]
Probably, we have to ask for a new forum section named "
VC++ 2012 & C++ REST SDK (codename Casablanca)". :)
[ on-topic ]
Have a look in ...\casablanca\include\cpprest\basic_types.h and you'll find
Code:
namespace utility
{
// ...
#ifdef _UTF16_STRINGS
//
// On Windows, all strings are wide
//
typedef wchar_t char_t ;
typedef std::wstring string_t;
#define _XPLATSTR(x) L ## x
typedef std::wostringstream ostringstream_t;
typedef std::wofstream ofstream_t;
typedef std::wostream ostream_t;
typedef std::wistream istream_t;
typedef std::wifstream ifstream_t;
typedef std::wistringstream istringstream_t;
typedef std::wstringstream stringstream_t;
#define ucout std::wcout
#define ucin std::wcin
#define ucerr std::wcerr
#else
//
// On POSIX platforms, all strings are narrow
//
typedef char char_t;
typedef std::string string_t;
#define _XPLATSTR(x) x
typedef std::ostringstream ostringstream_t;
typedef std::ofstream ofstream_t;
typedef std::ostream ostream_t;
typedef std::istream istream_t;
typedef std::ifstream ifstream_t;
typedef std::istringstream istringstream_t;
typedef std::stringstream stringstream_t;
#define ucout std::cout
#define ucin std::cin
#define ucerr std::cerr
#endif // endif _UTF16_STRINGS
// ...
}// namespace utility
So, to avoid conversion errors (and make cross-platform code) when deal with C++ REST SDK, use C++ REST SDK types:
utility::string_t instead of
std::string,
utility::ucin and not
std::cin, and so on.
In my case utility::ucin is not available. When std::cout << "Serving GET" << std::endl; is working fine, cin should work fine too but how will I convert that string to something similar to U("") ?
Re: Input strings in VC++ 2012
Quote:
Originally Posted by
maverick786us
In my case utility::ucin is not available.
Define "not available".
Re: Input strings in VC++ 2012
Quote:
Originally Posted by
VictorN
Define "not available".
When I type utility:: ucin is not there in the dropdown list of its members, as if its not a member of utility.
Re: Input strings in VC++ 2012
ucin is a definition (by a #define), not a class member so won't show in the dropdown list.
Re: Input strings in VC++ 2012
...and about C3493
Let's try to write a simple "Hello lambda!" program!
Code:
#include <iostream>
#include <string>
int main()
{
std::string hello_lambda = "Hello lambda!";
auto say_hello = [] {std::cout << hello_lambda; };
say_hello();
}
This generates compiler error C3493:
Code:
error C3493: 'hello_lambda' cannot be implicitly captured because no default capture mode has been specified
That's because the lambda-introducer ([]) is empty, meaning "nothing to capture".
To get rid of that error, we have to specify capture mode, as follows:
Code:
std::string hello_lambda = "Hello lambda!";
// hello_lambda variable is captured by value
auto say_hello = [hello_lambda] {std::cout << hello_lambda; };
say_hello();
or
Code:
std::string hello_lambda = "Hello lambda!";
// hello_lambda variable is captured by reference
auto say_hello = [&hello_lambda] {std::cout << hello_lambda; };
say_hello();
or
Code:
std::string hello_lambda = "Hello lambda!";
// all variables are captured by value
auto say_hello = [=] {std::cout << hello_lambda; };
say_hello();
or
Code:
std::string hello_lambda = "Hello lambda!";
// all variables are captured by reference
auto say_hello = [&] {std::cout << hello_lambda; };
say_hello();
or...
I would like to suggest: before copy-pasting then modifying code examples that use C++ lambda expressions, try to understand them.
There are tons of articles about C++11 new features including lambda over the Internet.
Just pointing to few examples:
May look weird but at last they are not so much "rocket science". ;)
Re: Input strings in VC++ 2012
...and about U("...")
U(x) is a macro, also defined in basic_types.h header file of C++ REST SDK.
Code:
#ifdef _MS_WINDOWS
#define _UTF16_STRINGS
#endif
// ...
#ifdef _UTF16_STRINGS
//
// On Windows, all strings are wide
//
// ...
#define _XPLATSTR(x) L ## x
// ...
#else
//
// On POSIX platforms, all strings are narrow
//
//...
#define _XPLATSTR(x) x
// ...
#endif // endif _UTF16_STRINGS
#ifndef _TURN_OFF_PLATFORM_STRING
#define U(x) _XPLATSTR(x)
#endif // !_TURN_OFF_PLATFORM_STRING
In a build for the Windows platform, U("...") expands to a wide string literal, L"...".
For the other platforms U("...") expands to a narrow string literal, "...".
Re: Input strings in VC++ 2012
Quote:
Originally Posted by
ovidiucucu
...and about
U("...")
U(x) is a macro, also defined in
basic_types.h header file of
C++ REST SDK.
Code:
#ifdef _MS_WINDOWS
#define _UTF16_STRINGS
#endif
// ...
#ifdef _UTF16_STRINGS
//
// On Windows, all strings are wide
//
// ...
#define _XPLATSTR(x) L ## x
// ...
#else
//
// On POSIX platforms, all strings are narrow
//
//...
#define _XPLATSTR(x) x
// ...
#endif // endif _UTF16_STRINGS
#ifndef _TURN_OFF_PLATFORM_STRING
#define U(x) _XPLATSTR(x)
#endif // !_TURN_OFF_PLATFORM_STRING
In a build for the Windows platform, U("...") expands to a
wide string literal, L"...".
For the other platforms U("...") expands to a
narrow string literal, "...".
So when I have declared this thing.
Code:
utility::string_t strName1;
ucin >> strName1;
How can I convert it into this U, so that i can use it here...
Code:
req.reply(status_codes::OK, U("input string!"), U("text/html"));
So that I can use it instead of hard coded "input string"?
Re: Input strings in VC++ 2012
Quote:
Originally Posted by
maverick786us
So when I have declared this thing.
Code:
utility::string_t strName1;
ucin >> strName1;
How can I convert it into this U, so that i can use it here...
Code:
req.reply(status_codes::OK, U("input string!"), U("text/html"));
So that I can use it instead of hard coded "input string"?
Why not?
Completing the suggestion from post #8: before copy-pasting then modifying code examples containing a call to C++ REST SDK function http_request::reply, try to find out what type of arguments it takes.
For that you can simply do the following:
- in the text code editor, right click on reply token and choose "Peek definition", "Go to definition" or "Go to declaration" or
- select reply token then hit F12 key or
- have a look into C++ REST SDK documentation, also available on-line http://microsoft.github.io/cpprestsd...a6c51e3203d577.
You will find something like this
Code:
pplx::task<void> reply(http::status_code status,
const utility::string_t &body_data,
utility::string_t content_type = _XPLATSTR("text/plain")) const
As long as the second argument is of type const utility::string_t&, you can pass strName1 which is of type utility::string_t, with not any explicit conversion.