File: lexical_cast.cpp

package info (click to toggle)
seqan2 2.4.0%2Bdfsg-16
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 224,180 kB
  • sloc: cpp: 256,886; ansic: 91,672; python: 8,330; sh: 995; xml: 570; makefile: 252; awk: 51; javascript: 21
file content (60 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>

#include <seqan/sequence.h>
#include <seqan/stream.h>

using namespace seqan;

int main()
{
    // interpret string literal as int
    int x = 0;
    // successful cast
    std::cout << "success=" << lexicalCast(x, "123");
    std::cout << ", x=" << x << "\n";
    // unsuccessful cast
    std::cout << "success=" << lexicalCast(x, "42a");
    std::cout << ", x=" << x << "\n";

    // interpret std::string as int
    std::cout << "success=" << lexicalCast(x, std::string("234"));
    std::cout << ", x=" << x << "\n";
    // interpret CharString as int
    std::cout << "success=" << lexicalCast(x, CharString("345"));
    std::cout << ", x=" << x << "\n";
    // interpret infix as int
    CharString str = "123";
    std::cout << "success=" << lexicalCast(x, infix(str, 1, 2));
    std::cout << ", x=" << x << "\n";

    // interpret literal as float and double
    float f = -1;
    double d = -1;
    std::cout << "success=" << lexicalCast(f, "3.1");
    std::cout << ", f=" << f << "\n";
    std::cout << "success=" << lexicalCast(d, "4.2");
    std::cout << ", d=" << d << "\n";

    // demonstrate throwing of exceptions with lexicalCast()
    try
    {
        x = lexicalCast<int>("a");
    }
    catch (BadLexicalCast const & badCast)
    {
        std::cout << "error: " << badCast.what() << "\n";
    }

    // demonstrate throwing of exceptions with lexicalCastWithException
    try
    {
        lexicalCastWithException(x, "b");
    }
    catch (BadLexicalCast const & badCast)
    {
        std::cout << "error: " << badCast.what() << "\n";
    }

    return 0;
}